将 lodash 用于 MobX 存储

Using lodash for a MobX store

我正在使用 lodashmobx 来检索数据,看起来像这样:

_.get(store, "prop.arr[0].subProp")

工作正常,但 mobxarrundefined 或空的情况下给我警告:

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

有什么解决方法吗?我不想添加长度检查,希望将其保留为一行。

在将 store 传递给 get 之前,通过 mobx 传递 toJS 实用程序。这应该可以解决它。

所以你的一个班轮将是 - _.get(toJS(store), "prop.arr[0].subProp")

更多关于 toJS 的信息 - https://mobx.js.org/refguide/tojson.html