reactjs - 关于redux组件渲染方法的问题
问题描述
我有三个组件,它们都连接到 redux 存储并显示相同的状态,我们称之为x
并且三个连接的组件具有父子关系,例如:
- connect()(ComponentA)
- connect()(ComponentB)
- connect()(ComponentC)
ComponentB 和 ComponentC 都是 ComponentA 的子级
当我改变x
连接的ComponentC的值时x
,ComponentA和ComponentB的显示也发生了变化,但是ComponentB中的render方法调用了多少次?
理论上它应该调用两次(一次由父组件 A 调用,一次由自身调用),实际上它只调用一次。为什么?
还原代码:
function counter(initalState = { count: 0 }, action) {
switch (action.type) {
case "add":
return Object.assign({}, { count: initalState.count + 1 });
default:
return initalState;
}
}
const store = createStore(
combineReducers({
counter
})
);
function mapStateToProps(state) {
return {
count: state.counter.count
};
}
function mapDispatchToProps(dispatch) {
return {
add: function() {
dispatch({
type: "add"
});
}
};
}
解决方案
推荐阅读
- ios - 如何在IOS中将视频文件转换为Base64字符串?
- android - 我们可以创建双击关闭屏幕应用程序吗?
- vba - 如果满足条件,VBA循环遍历不同的工作表并将行粘贴到主工作表中
- python - python:打印非继承方法
- c - 试图理解 pthread_cond_lock 和 pthread_cond_signal
- react-native - 使用 React-Native-Video 在音乐播放器中反应原生随机播放歌曲
- ios - 在 IOS 目标 C 中使用高度约束未正确隐藏视图的布局
- c - C Linux内核模块钩子stat隐藏目录
- verilog - Verilog 合成在 2000 次迭代后未收敛
- c - 如何在c中使用ffmpeg读取数据?