Reactjs React Mobx-从其他存储更改存储,而不是渲染观察者组件
我必须通过一个商店的Reactjs React Mobx-从其他存储更改存储,而不是渲染观察者组件,reactjs,mobx,mobx-react,Reactjs,Mobx,Mobx React,我必须通过一个商店的@action函数更新@observable变量。 当我从component类调用此@action时,它会更改@observable变量并重新呈现@observable组件。 但是,当我尝试从其他存储中的@action调用此@action时,通过使用回调,@observate变量会发生更改,但@observate组件不会重新渲染 一家商店: class OneStore { @observable variable; @action setVariabl
@action
函数更新@observable
变量。
当我从component
类调用此@action
时,它会更改@observable
变量并重新呈现@observable
组件。
但是,当我尝试从其他存储中的@action
调用此@action
时,通过使用回调,@observate
变量会发生更改,但@observate
组件不会重新渲染
一家商店:
class OneStore {
@observable variable;
@action
setVariable(value) {
this.variable = value;
}
}
其他商店:
class OtherStore {
@action
setVariable(callBack, value) {
callBack(value); //callBack is oneStore.setVariable
}
}
@observer
组件:
@inject('oneStore')
@observer
class ObserverComponent extends Component {
render() {
if (this.props.oneStore.variable) {
return (
<div> {this.props.oneStore.variable} <div/>
);
} else { return null; }
}
}
@inject('oneStore')
@观察者
类ObserverComponent扩展了组件{
render(){
if(this.props.oneStore.variable){
返回(
{this.props.oneStore.variable}
);
}else{return null;}
}
}
我还尝试通过
@computed
get函数获取变量,但它仍然没有重新呈现。我尝试将绑定添加到操作-@action.bound
中,它只是工作
我认为OneStore的this
在嵌套回调后被破坏了
正确的代码是:
class OneStore {
@observable variable;
@action.bound
setVariable(value) {
this.variable = value;
}
}
请附上一个带有示例的提琴/沙盒虽然不建议使用这种架构,但这是一个正确的答案