有问题请教老师
来源:8-5 【更新state】Action与Reducer处理

粉墨登场
2021-11-15
老师您好,redux 中的 reducer 在第一次默认初始化的时候 state 是默认的 defaultState,接着返回 state 作为 store 的初始数据(我试过把返回旧的 state 删掉,在 createStore() 时会报错,因此会有这样的理解),这时 store 就把这条数据保存在全局仓库中了。当组件 dispatch action 时,reducer 中的第一个形参 state 就是 store 给 reducer 的了,也就是旧的 state。是这样吗?
export default (state = defaultState, action) => {
if (action.type === 'changeLanguage') {
const { payload } = action;
const newState = {...state, language: payload};
return newState;
};
// 这一句如果删掉, const store = createStore(languageReducer) 这里会报错
return state;
}
写回答
1回答
-
同学你好,你说的没错,dispatch action的时候,参数中的state是旧数据,而根据旧数据(state)产生新数据(state)则是reducer的工作。
112022-01-25
相似问题