web*_*stx 6 reactjs redux react-redux
为什么我的 store.getState 返回未定义的值!我对 ReactJS 和 Redux 很陌生,这里是我的代码
\n\nimport {createStore} from \'redux\';\n\nconst initialState = {\nsalary: 1000,\n}\n\nconst reducer = (state=initialState, action) => {\nswitch (action.type) {\n case "ADD":\n state = {\n salary: state.salary+=action.payload,\n }\n break;\n\n default:\n break;\n}\n}\n\nconst store = createStore(reducer);\n\nstore.subscribe(() => {\nconsole.log(\'Update salary: \', store.getState.salary);\n})\n\nstore.dispatch({\ntype: "ADD",\npayload: 2000,\n});\n
Run Code Online (Sandbox Code Playgroud)\n\n控制台:更新工资:\xe2\x80\x93 undefined
\n\n谢谢。
\ngetState
是一个方法,调用它
console.log('Update salary: ', store.getState().salary);
Run Code Online (Sandbox Code Playgroud)
你甚至应该修理你的减速机
case "ADD":
return {salary: state.salary + action.payload}
Run Code Online (Sandbox Code Playgroud)
它必须返回新状态,你不返回任何东西
归档时间: |
|
查看次数: |
11332 次 |
最近记录: |