我希望使用redux来存储整个react应用的状态,但是我在一个特殊情况下遇到了困难:
- 当组件需要本地状态并由生命周期方法(如
componentDidUpdate
或componentDidMount
)修改时,该怎么办?
以下是包含由isotope布局库排列的“卡片”的React组件示例:
componentDidMount() {
let container = ReactDOM.findDOMNode(this);
if (! this.state.isotope) {
this.setState({ isotope: new Isotope(container, {itemSelector: '.grid-item', layoutMode: 'masonry'})});
}
}
componentDidUpdate(new_props, new_state) {
if (new_state.items_list != this.state.items_list) {
if (this.state.isotope) {
this.state.isotope.reloadItems();
this.state.isotope.layout();
this.state.isotope.arrange();
}
}
}
有没有一种方法可以删除此组件中的本地状态,并改用redux?我不知道如何做到这一点。