我在我的React应用程序中引入了Mobx。该应用程序没有使用任何框架(Flux Redux等)。
在每个组件上,我首先使用inject
包的mobx-react
装饰器注入状态:
const React = require('react');
import {observer, inject} from 'mobx-react';
@inject('myState') @observer
class MyComponent extends React.Component{...}
在更新时,我直接更改组件的onChange方法的状态,状态包含一个对象数组(blocks
),每个块都有一些特定的属性:
onChange = (event) => {
this.props.myState['blocks'][this.props.index][event.target.name] = event.target.value;
}
然后,我使用从状态获取的值渲染组件。
render = () => {
const value = this.props.myState['blocks'][this.props.index]['myInput'];
console.log('re rendering...');
return (
<div>
<textarea value={value}
onChange={this.onChange} />
</div>
);
}
但实际上,在onChange
执行并且状态myState
发生变化后,似乎没有触发渲染功能。
即使组件是州订户,也不会触发有关如何重新呈现的任何建议?