I got this waring message from Mobx.
[mobx.array] Attempt to read an array index (0) that is out of bounds (0). Please check length first. Out of bound indices will not be tracked by 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
}
How can I remove that warning? However, this code has no problem. It works well.
I'm using selectAllChecks function by onChange function.
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>
I have to 4 version for IE.
"mobx": "^4.1.0",
"mobx-react": "^5.2.6",
Is there any other solution?
this.checks[target].length > 0before comparing lengths? It looks like you are reading an index before the observable has acknowledged new data, as the comparison of lengths shown above could always return true if the compared array has not collected any items. I am more familiar with MST and have not used direct mobx-react much. - Nicholas Pesathis.checks[target].lengthas you said. But the warning is still occuring. thanks - kkangilchecksobj, cause it is not an array. Check where do you have an observable array in the code. - felixmosh