javascript - 无法初始化商店的状态
问题描述
我确定我已经初始化了我的 redux 商店的状态,但是,我总是不确定
在 const 中初始化状态,然后是 reducer,然后是 store。我做了 console.log(store) 因为当我将商店提供给应用程序时我得到了未定义
const init = {
counter:0
};
//reducer
const mainReducer= (state=init, action)=>{
switch(action.type){
case 'INC_COUNTER':
return {...state, counter:state.counter+1}
case 'de':
return {counter:-1}
}
}
const Appstore = Redux.createStore(mainReducer);
console.log("this is your current state",Appstore.getState())```
解决方案
您似乎没有返回默认状态。请注意,redux 存储将使用 INIT 操作进行初始化,在这种情况下,您将返回 undefined,因为操作类型既不是 INC_COUNTER 也不是 de
您应该始终处理默认情况,例如
const mainReducer= (state=init, action)=>{
switch(action.type){
case 'INC_COUNTER':
return {...state, counter:state.counter+1}
case 'de':
return {counter:-1}
}
return state;
}
推荐阅读
- python - 同时使用 pytest 和 tf.test.TestCase 的问题
- apache-nifi - Apache Nifi PutFTP 更改文件扩展名
- python-3.x - 训练机器学习模型不分类错误输入或空输入
- vue.js - Bootstrap Vue - 将卡片数据作为有效负载传递
- qt - libusb 连接到设备正常,中断传输导致 LIBUSB_ERROR_IO
- r - 条形图 - 计数变量显示频率而不是数据列中的值
- reactjs - 在 React 应用程序中加载动态生成的图像
- python - Couchbase 查询不返回不允许参数
- google-apps-script - Google 应用程序脚本的数据分析库
- r - 为什么我在 R 中使用“放置”包时得到“OVER_QUERY_LIMIT”