状態が変更されたときに自動的に表示を再描画することに問題があります。 状態は変更されましたが、render()
は呼び出されません。しかし、私がthis.forceUpdate()
と呼ぶとき、すべては大丈夫ですが、私はそれが最善の解決策ではないと思います。 誰かが私を助けることができますか?状態が変更されたときに反応してレンダリングが呼び出されないのはなぜですか?
class TODOItems extends React.Component {
constructor() {
super();
this.loadItems();
}
loadItems() {
this.state = {
todos: Store.getItems()
};
}
componentDidMount(){
//this loads new items to this.state.todos, but render() is not called
Store.addChangeListener(() => { this.loadItems(); this.forceUpdate(); });
}
componentWillUnmount(){
Store.removeChangeListener(() => { this.loadItems(); });
}
render() {
console.log("data changed, re-render");
//...
}}
'this.state' sh ouldは直接突然変異することはありません。 – jered
@JCD私の間違いは、React Nativeから来ていますが、 'this.state = {}'を使ってコンストラクタから初期状態を宣言することができます。 –
@JCD実際には、[React docs](https://facebook.github.io/react/docs/reusable-components.html)によると、これは完全に正当なものであり、少なくともそこに使用されています(「ES6 Classes in 2番目のコードブロック) –