我从Mobx收到了这条警告信息。
[mobx.array]尝试读取超出范围(0)的数组索引(0)。请先检查长度。 MobX不会跟踪超出范围的索引
@observable checks = {
deviceType: ['phone','laptop', ...],
deviceTypeChecks: [],
...
}
@action
selectAllChecks = (target, type) => {
const targetChecks = []
if (this.checks[target].length !== this.checks[type].length) {
this.checks[target].forEach(el => targetChecks.push(el))
}
this.checks[type] = targetChecks
}
我该如何删除该警告?但是,这段代码没有问题。它运作良好。
我正在通过onChange函数使用selectAllChecks
函数。
const {
deviceType,
deviceTypeChecks
} = this.props.store.checks
<label className="mr10">
<input
type="checkbox"
checked={deviceType.length === deviceTypeChecks.length}
onChange={() =>
selectAllChecks('deviceType', 'deviceTypeChecks')
}
/>
<span>All device type</span>
</label>
IE必须有4个版本。
"mobx": "^4.1.0",
"mobx-react": "^5.2.6",
还有其他解决方案吗?
如果你改变你的@action
会发生什么:
@action
selectAllChecks = (target, type) => {
this.checks[type] = this.checks[target].map((value) => value);
}
这仍然显示mobx out of bounds
错误?