这是代码 - 非常确定它是关于extendObservable的东西,我只是没有得到,但现在已经盯着它看了很长一段时间.当addSimpleProperty运行时,它似乎更新了对象,但它不会触发渲染.
const {observable, action, extendObservable} = mobx;
const {observer} = mobxReact;
const {Component} = React;
class TestStore {
@observable mySimpleObject = {};
@action addSimpleProperty = (value) => {
extendObservable(this.mySimpleObject, {newProp: value});
}
}
@observer
class MyView extends Component {
constructor(props) {
super(props);
this.handleAddSimpleProperty = this.handleAddSimpleProperty.bind(this);
}
handleAddSimpleProperty(e) {
this.props.myStore.addSimpleProperty("newpropertyvalue");
}
render() {
var simpleObjectString =JSON.stringify(this.props.myStore.mySimpleObject);
return (<div>
<h3> Simple Object</h3>
{simpleObjectString}
<br/>
<button onClick={this.handleAddSimpleProperty}>Add Simple Property</button>
</div>);
}
}
const store = new TestStore();
ReactDOM.render(<MyView myStore={store} />, document.getElementById('mount')); …Run Code Online (Sandbox Code Playgroud)