【发布时间】:2020-06-01 07:26:25
【问题描述】:
我正在使用 React、Redux 和 React-Redux Provider 函数
const mapStateToProps = (store: any) => {
console.log("mapStateToProps", store.textTwo);
return store.textTwo;
};
如果我将上面的mapStateToProps 函数使用到react-redux 的connect 方法并更新状态,它将console.log 更改mapStateToProps 函数而不是重新渲染组件,甚至不会出现componentWillReceiveProps 钩子。
但是当我添加spread operator 即改变mapStateToProps 返回数据时,它将重新渲染完整的组件。
const mapStateToProps = (store: any) => {
console.log("mapStateToProps", store.textTwo);
return { ...store.textTwo }; <-- this will re render complete component
};
如何在不重新渲染完整组件的情况下将状态传递给组件的 props。
--- 更新---
样本状态数据
store = {
textTwo: {
headerText: 'Sample',
list: [
{
id: 1,
name: 'test 1'
}
]
},
text: {}
}
headerText 是显示标题标题的子组件。
list 是我正在迭代并列出名称的数据数组。
尝试了以下解决方案(由@Siva Kondapi Venkata 提供)并且它正在工作,但是如果我在列表中添加一个新项目,标题组件也会重新渲染。
const mapStateToProps = (store: any) => ({
textTwo: store.textTwo
});
【问题讨论】:
标签: javascript reactjs redux react-redux