【发布时间】:2023-04-07 07:02:02
【问题描述】:
当状态改变时,我遇到了自动重新渲染视图的问题。
状态已更改,但未调用 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");
//...
}}
【问题讨论】:
标签: javascript reactjs flux