【发布时间】:2021-11-21 10:29:36
【问题描述】:
我在更改我所在州的值时遇到问题。我有
state = {
......,
tags: []
}
这些标签的填充方式如下:
tags: this.props.userStore.details.tags
标签是字典列表:
tags = [{'name': tag1, 'userSelect': true}, {'name': tag2, 'userSelect': false}]
我将它们全部显示在复选框中:
renderTagCheckbox() {
return this.state.tags.map((item, key) => {
return(
<TouchableOpacity key={key} style={{flexDirection: "row", marginTop: 5, justifyContent: "center"}}>
<CheckBox
isChecked={item.userSelect}
onClick={() => this.changeCheckboxValue(item)}
/>
<Text>{item.name}</Text>
</TouchableOpacity>
)
})
}
在我的changCheckboxValue() 我有:
changeCheckboxValue(item) {
//this.refersSubscriptions.forEach(s => s.remove())
let currentTags = this.state.tags
currentTags.forEach(tag => {
if (tag===item) {
tag.userSelect = !tag.userSelect
}
})
}
这样我想我可以用currentTags setState 来改变值,但我得到一个错误:
[mobx-state-tree]Cannot modify 'tags@/userStore/details/tags/0', the object is protected and can only be modified by using an action.
有人能告诉我我做错了什么吗?
【问题讨论】:
-
你能分享产生错误的代码sn-p吗?
-
@novonimo 问题出在
changeCheckboxValue- 当我尝试tag.userSelect = !tag.userSelect时出现错误 -
您是否使用“useState”挂钩来创建您的状态?你用的是类组件还是函数组件?
-
@novonimo 我正在使用没有 useState 的类组件
-
您的标签是否存储在全局存储中?
标签: react-native setstate mobx-state-tree