reactjs - TypeError:无法读取开关中未定义的属性“类型”(action.type)
问题描述
我第一次尝试关注 youtube 上关于 redux 的视频,但不知道为什么我得到了undefined
。我已经尝试在谷歌上寻找答案,但仍然无法弄清楚。
import { createStore } from 'redux';
//action
const up = () => {
return {
type: 'up'
}
}
const down = () => {
return {
type: 'down'
}
}
//reducer
const counter = (state = 0, action) => {
switch(action.type){
case 'up':
return state + 1;
case 'down':
return state - 1;
}
}
let store = createStore(counter());
store.subscribe(() => console.log(store.getState()))
store.dispatch(up())
store.dispatch(down())
谢谢您的帮助。
解决方案
您必须像这样传递实际函数createStore
:
let store = createStore(counter);
这样,您将传递 的实际函数定义counter
,而不仅仅是返回状态。
推荐阅读
- javascript - 如何删除引导行内的额外空间
- linux - 在多个远程服务器上通过 SSH 运行命令的 shell 脚本
- javascript - 交换变量 JS
- python - 使用不记名令牌和刷新令牌从 api 请求数据
- javascript - 是否有任何函数可用于使用 React Js Hook 查看新更新的数组状态?
- reactjs - 无法在不破坏父反应应用程序样式的情况下导入使用 Material UI 实现的 React 组件
- angular - 新窗口工作完成后重定向到旧窗口
- weblogic - 连接到 OEPE 中的 Oracle Weblogic Cloud 12c 服务器时出现异常
- javascript - 在创建期间设置数组长度是否有好处?
- sql - 从 2 个表中过滤并加入 2 个其他表