我必须通过一家商店的@observable
功能更新@action
变量。当我从@action
类中调用此component
时,它将更改@observable
变量并重新呈现@observer
组件。但是,当我尝试通过使用回调从other store中的@action
调用此@action
时,@observable
变量会更改,但@observer
组件不会重新呈现。
一家商店:
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; }
}
}
我也尝试通过@computed
get函数来获取变量,但仍然无法重新渲染。
我试图添加绑定到动作-@action.bound
,它确实起作用...
我认为嵌套回调后,OneStore的this
被破坏了。
正确的代码是:
class OneStore {
@observable variable;
@action.bound
setVariable(value) {
this.variable = value;
}
}