更换道具后无需重新渲染组件

时间:2019-01-14 14:59:10

标签: reactjs redux react-select

所以我有减速器autocomplete,它存储了react-select加载的所有选项。我也有两个组成部分-<Parent /><Modal />。他们两个都连接到redux状态以接收autocomplete道具,因为它们都有自己的<AsyncSelect />组件。

Select<Parent />组件的子代,它从减速器autocomplete.labels接收选项。第二个Select组件是<Modal />的子组件,它从减速器中接收选项:autocomplete.differentLabels

现在,当我使用Select的子级Modal时,即使没有依赖于Parent的jsx,也会重新渲染整个autocomplete组件。这是预期的行为吗?由于重新渲染,性能下降。

也许我应该将labelsdifferentLabels分成单独的减速器?

1 个答案:

答案 0 :(得分:0)

一种可能的解决方案是使用shouldComponentUpdate将当前道具与以前的道具进行比较,并在其中做一些逻辑以防止不必要的更新。

shouldComponentUpdate(nextProps, nextState) {
    if (this.props.myProp !== nextProps.myProp) {
        // Your logic
        return false;
    }
    return true;
}
相关问题