Reactjs Mobx状态树不';在新地图参考上重新渲染
对mobx状态树来说是很新的。无论何时更改我的模型以便types.map获得新引用,我的组件都不会重新提交。 在我的示例中,更改模型上的字符串确实会触发重新加载。 我正在读取我希望重新渲染的组件中的这两个属性,并用Reactjs Mobx状态树不';在新地图参考上重新渲染,reactjs,mobx,mobx-react,mobx-state-tree,Reactjs,Mobx,Mobx React,Mobx State Tree,对mobx状态树来说是很新的。无论何时更改我的模型以便types.map获得新引用,我的组件都不会重新提交。 在我的示例中,更改模型上的字符串确实会触发重新加载。 我正在读取我希望重新渲染的组件中的这两个属性,并用@observer对其进行了修饰 下面是一个Stackblitz示例: 第37行中的注释使其生效 型号: export const treeModel = types .model({ tree: types.map(types.string), tes
@observer
对其进行了修饰
下面是一个Stackblitz示例:
第37行中的注释使其生效
型号:
export const treeModel = types
.model({
tree: types.map(types.string),
test: ''
})
.actions(self => ({
setTree(tree) {
self.tree = tree
},
setTest(data) {
self.test = data;
}
}));
应用程序
类应用程序扩展组件{
树顶;
构造函数(){
超级();
此.state={
名称:“反应”
};
this.treeStore=treeModel.create();
this.treeStore.setTree(newmap());
设置超时(()=>{
this.treeStore.setTree(newmap());
//启用此选项将重新触发测试组件的渲染
//this.treeStore.setTest('azerty');
}, 2000);
}
render(){
返回(
开始编辑以查看发生的奇迹:)
);
}
}
测试组件
@observer
class Test extends Component {
constructor(props) {
super(props);
}
render() {
console.log('rendered');
const {tree, test} = this.props.store;
return <div>test</div>
}
}
@observer
类测试扩展了组件{
建造师(道具){
超级(道具);
}
render(){
console.log('rendered');
const{tree,test}=this.props.store;
回归试验
}
}
类型。map
不是您常规的映射(尽管它有类似的方法),虽然期望它可以接受映射是合乎逻辑的,但显然它没有(也许您应该在他们的网站上提交一个关于它的问题)。但是,要解决眼前的问题,只需在setTree
操作或数组的-array中传递一个对象:
this.treeStore.setTree({ 'one': '1' });
或
你的树顶应该是可观察的
class App extends Component {
@observable treeStore;
}
在我的实际示例中,我不能使用对象,因为我的地图有“非字符串”键。不幸的是,这对我来说不是一个真正的选择。答案是Thx。你可以使用数组数组。我提到了它,现在用一个例子更新了我的答案。
this.treeStore.setTree([[ 'one', '1' ]]);
class App extends Component {
@observable treeStore;
}