我将redux-persist
与Redux结合使用,以保持应用重启之间的状态。当前,在开发版本上,它永远不会保持状态。
我的商店
const persistConfig = {
key: 'root',
storage: AsyncStorage,
blacklist:['users', 'activity', 'meeting', 'workshop', 'predavanja', 'klizni', 'meetings', 'standing']
}
const allReducers = combineReducers({
darkMode: persistReducer(persistConfig, darkReducer),
users: userReducer,
activity: activity,
meeting: meeting,
workshop: workshop,
predavanja: predavanja,
klizni: klizni,
meetings: meetings,
standing: standing,
});
我一直在记录要保留的状态,但是它一直返回到默认状态。
答案 0 :(得分:1)
是否有任何原因,您决定将配置中的每个减速器列入黑名单?如果您试图保持所有Redux状态,则不应定义任何黑名单。此外,我看不到您如何创建自己的redux存储,但它看起来应该像
import { createStore } from 'redux';
import { persistStore, persistReducer } from 'redux-persist';
...
const store = createStore(persistReducer(persistConfig, allReducers));
const persistor = persistStore(store);
在您的根组件中,应按如下所示注入商店:
import { PersistGate } from 'redux-persist/integration/react';
import { Provider } from 'react-redux';
...
return (
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
.....
</PersistGate>
</Provider>
);
Reducer应该类似于以下代码片段:
const persistConfig = {
key: 'root',
storage: AsyncStorage
}
const allReducers = combineReducers({
darkMode: darkReducer,
users: userReducer,
activity: activity,
meeting: meeting,
workshop: workshop,
predavanja: predavanja,
klizni: klizni,
meetings: meetings,
standing: standing,
});
如果遵循所有这些步骤,则整个持久存储区都应该保留。