猿问

redux-persist合并reducer时报错

问题描述
报错信息:
Storedoesnothaveavalidreducer.MakesuretheargumentpassedtocombineReducersisanobjectwhosevaluesarereducers.
问题出现的环境背景及自己尝试过哪些方法
我将不同功能的reducer放到不同的文件夹下,然后使用redux-persist进行数据持久化处理,web项目会经常刷新么,所以想让store中的数据不丢失,然而合并reducer应用combineReducers方法是报错,不知道为什么
相关代码
//请把代码文本粘贴到下方(请勿用图片代替代码)reducer
import*asPagesfrom'./action-types';
letdefaultValue={
user_name:"",
user_id:""
}
exportdefaultfunctionUserInfo(state=defaultValue,action={}){
switch(action.type){
casePages.SAVEUSERINFO:
return{...state,
...{
user_name:action.data.data.username,
user_id:action.data.data.id
}
};
default:
returnstate;
}
}
合并位置
import{
combineReducers,
}from'redux';
importUserInfofrom'./Pages/reducers';
importSaveInfofrom'./Layout/reducers';
constrootReducer=combineReducers({
UserInfo,
SaveInfo
});
exportdefaultrootReducer;
入口文件
import{
Provider
}from'react-redux';
import{
createStore,
applyMiddleware
}from'redux';
//这个Reducers就是我合并的reducer
importReducersfrom'@/Reducers';
importthunkfrom'redux-thunk';
import{persistStore,persistCombineReducers}from'redux-persist';
import{PersistGate}from'redux-persist/es/integration/react';
importstoragefrom'redux-persist/es/storage'
constconfig={
key:'root',
storage,
};
functionconfigureStore(){
console.log(Reducers)
letreducer=persistCombineReducers(config,Reducers);
letstore=createStore(reducer,applyMiddleware(thunk));
letpersistor=persistStore(store);
return{persistor,store}
}
constrender=Component=>{
const{persistor,store}=configureStore();
ReactDOM.render(
//绑定redux、热加载
,
document.getElementById('app'),
)
}
你期待的结果是什么?实际看到的错误信息又是什么?
期待无论怎么刷新浏览器,刷新之前的store中的数据能保存到刷新之后来
慕侠2389804
浏览 1288回答 2
2回答

德玛西亚99

persistCombineReducers这个方法不知道是我用的不对还是被官方废弃了,看文档使用persistReducer方法来合并config和reducer即可完成实现import{persistStore,persistReducer}from'redux-persist';constpersistedReducer=persistReducer(persistConfig,Reducers)functionconfigureStore(){letstore=createStore(persistedReducer,applyMiddleware(thunk));letpersistor=persistStore(store);return{persistor,store}}constrender=Component=>{const{persistor,store}=configureStore();ReactDOM.render(//绑定redux、热加载,document.getElementById('app'),)}哪位大神如果有具体的问题原因也烦请告知一下,感激不尽
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答