reactjs - 保持 redux store 状态不变性的最佳方法是什么?
问题描述
/reducers/index.js
import * as types from '../actions/ActionTypes';
var initialState = {
categoryName: 'default name redux'
};
function re_modifyCategoryName(state=initialState, action) {
switch(action.type) {
case types.MODIFY_CATEGORY_NAME :
return Object.assign({}, state, {
categoryName: action.categoryNewName
});
default:
return state;
}
}
export {re_modifyCategoryName}
我肯定检查state
的redux store
是由 Redux-dev-tool 和 redux-logger 更新的,
但mapStateToProps
没有被调用。
PresentationalComponent.js
...
const mapStateToProps = (state) => {
console.log("inside category name", state.categoryName);
return {categoryName: state.categoryName}
};
export default connect(mapStateToProps)(AnswerEachCategory);
...
当页面第一次呈现时,如您所见mapStateToProps
,"inside category name", state.categoryName
显示在console
并presentational component
正确地props.categoryName
从initialState
of 中获取redux store
。但是,为什么redux store
没有触发变化mapStateToProps
?
这个问题与state
redux 商店的不变性有关吗?
容器.js
...
const mapDispatchToProps = (dispatch) => ({
onModifyCategoryName: (categoryNewName) => {
dispatch(actions.modifyCategoryName(categoryNewName))
}
})
export default connect(null, mapDispatchToProps)(ModifyCategoryNameModal)
...
解决方案
推荐阅读
- css - 如何在没有动画的情况下切换 scaleX 来翻转图像
- python - 使用具有预期样式的 python 生成 yaml 文件
- inno-setup - 禁止在 Inno Setup 自定义页面上输入空白和仅空格
- python - Django/Nodejs - 上传大块文件
- javascript - Infinispan 的客户端 JS 未找到 Infinispan Java 客户端输入的缓存
- javascript - 使用“transform”样式属性重新排序特定的 DIV
- telegram-bot - 如何使用 answerInlineQuery Telegram 机器人发送 inlineKeyboard
- r - 如何手动输入节点 ID 以在 visNetwork 中选择/突出显示节点
- c# - 当视频上传大小为 30MB 但代码适用于 1MB 时,读取 MIME 多部分正文部分时出错?
- php - 如何生成字符串的排列,但没有重复和不同的长度?