小编etu*_*dor的帖子

Mobx-State-Tree-分配给数组类型

我有这个基本模型。

const stuff = types.model({
  term: types.string,
  excludeTerm: types.string,
  stores: types.array(types.string)
}).actions(self => ({
  setTerm(term: string) {
    self.term = term
  },
  setExcludeTerm(term: string) {
    self.excludeTerm = term
  },
  setStores(stores: string[]) {
    self.stores = stores   // <<< the lint error is on this line
  }
}))
Run Code Online (Sandbox Code Playgroud)

我收到以下TS Lint错误:

Type 'string[]' is not assignable to type 'IMSTArray<ISimpleType<string>> & IStateTreeNode<IArrayType<ISimpleType<string>>>'. Type 'string[]' is missing the following properties from type 'IMSTArray<ISimpleType<string>>': spliceWithArray, observe, intercept, clear, and 4 more.ts(2322)

这是一个令人讨厌的错误。我可以这样分配来修复它:(self as any).stores = stores …

typescript tslint mobx mobx-react mobx-state-tree

4
推荐指数
3
解决办法
1300
查看次数

Mobx-state-tree 在树内使用 mobx 反应 - 好还是坏实践?

我有一篇文章是 mobx-state-tree 对象,我在 React 应用程序中使用它。

这是我的树内的一个动作

setId(id: string) {
  self.id = id

  this.updateProduct()
},
Run Code Online (Sandbox Code Playgroud)

还有活动

 <input
  value={comp.productId}
  onChange={(e) => comp.setId(e.target.value)}
/>
Run Code Online (Sandbox Code Playgroud)

问题是this.updateProduct()每次更改都会运行,并在每次按键后进行异步调用。

我想利用 mobx 反应并使用类似的东西

reaction(
() => ({
  id: this.id
}),
() => {
  this.updateProduct()
}, {
  delay: 500 // this is the key thing
})
Run Code Online (Sandbox Code Playgroud)

我发现延迟在这种情况下非常有用,所以我想在树内使用它们。

在 mobx 状态树中添加反应是一个好习惯吗?如果是,使用反应的正确位置在哪里?

我可以在反应组件内定义反应,但它将在树之外。在树外面是个好习惯吗?

reactjs mobx mobx-react mobx-state-tree

2
推荐指数
1
解决办法
1036
查看次数

标签 统计

mobx ×2

mobx-react ×2

mobx-state-tree ×2

reactjs ×1

tslint ×1

typescript ×1