reactjs - React Mobx - 从其他商店更改商店而不渲染观察者组件
问题描述
我必须在一家商店@observable
按功能更新变量。@action
当我@action
从component
类中调用它时,它会更改@observable
变量并重新渲染@observer
组件。但是当我尝试@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
它很简单......
我认为this
OneStore 在嵌套回调后被破坏了。
正确的代码是:
class OneStore {
@observable variable;
@action.bound
setVariable(value) {
this.variable = value;
}
}
推荐阅读
- python-3.x - 用于文本分类的一类 SVM 模型(scikit-learn)
- r - 在R中如何使用ggplot绘制正态分布的尾部区域?
- r - 致命错误:无法创建 R_TempDir(在 Ubuntu 中安装 R 包)
- eclipse - 如何使用固定 IP 地址或系统变量设置 DOCKER_HOST?
- sql-server - 为什么我的查询在失败时会出现在案例中?
- python - 检测数据框中的空值在 Microsoft Excel 中显示为零
- arrays - 堆叠形状不等的数组
- excel - 检查单元格的值时类型不匹配
- python - 如何将给定列表的值放入现有字典中?
- botframework - 在通知中发送英雄卡或自适应卡