在我的React Native应用程序中,我遇到的情况是组件的一个特定子项,我render应该收到绿色或红色borderColor.
现在,我不想在我styles的这两种情况下创建两个单独的条目,因为它们只在borderColor属性上有所不同.
我的想法是从我styles喜欢的那些中得到合适的样式对象:
const styles = StyleSheet.create({
amountSection: {
borderWidth: 1,
flex: 1,
flexDirection: 'row',
borderRadius: 3
}
})
render() {
const amountBorderColor = this.state.isClaim ? 'green' : 'red'
const amountStyles = {
...styles.amountSection,
borderColor: amountBorderColor
}
return (
// ... apply amountStyles at appropriate component
)
}
Run Code Online (Sandbox Code Playgroud)
但是,此代码会出现以下错误:
未处理的JS异常:在此环境中,assign的源必须是一个对象.此错误是性能优化而不是规范兼容.
显然,错误是在我定义的行上命中的amountStyles.谁知道为什么会这样?我的语法有问题吗?我使用...表示法从现有对象创建一个新对象,并为其添加一些其他属性.