【发布时间】:2018-05-08 19:00:12
【问题描述】:
我有一个使用 Provider 包装的应用程序。
在应用程序的更深处,我使用的组件通常是独立的并且有自己的 Provider。
当“主”商店更新时,我想将信息传递给内部商店。
当我这样做时,我收到了来自 mobx 的警告说 -
Mobx Provider: Provider store 'internalStore' has changed. Please avoid replacing stores as the change might not propagate to all children.
我可以想到两种解决方案 - 1.将存储(internalStore)存储在RootStore中(双关语) 2.给internalStore添加更新方法
应用
const App = (props) => {
return (
<Provider store={store}>
App...
</Provider>
)
}
内部组件-
const Internal = (props) => {
return (
<Provider store={internalStore}>
<ReusedCompoenent />
</Provider>
)
}
你怎么看?想法?
【问题讨论】:
-
如果您想在
rootStore中的某些内容发生更改时更新您的internalStore,难道您不能将两个商店都放在最顶层的提供程序中并在internalStore中使用计算值派生自rootStore?中的值? -
在 mobx 文档中,有一点关于合并商店可能会对您有所帮助:mobx.js.org/best/store.html#combining-multiple-stores