将lodash用于MobX商店

问题描述 投票:0回答:1

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

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

工作正常,但mobx给我警告arrundefined或空:

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

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

javascript reactjs lodash mobx
1个回答
2
投票

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

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

更多关于toJS在这里 - https://mobx.js.org/refguide/tojson.html

© www.soinside.com 2019 - 2024. All rights reserved.