reactjs – 如何使用redux-persist保持React-native redux状态?
发布时间:2020-12-15 16:19:06 所属栏目:百科 来源:网络整理
导读:我一直在尝试使用redux-perist将我的redux-state保存到AsyncStorage.虽然我一直收到错误: _this.store.getState is not a function 我不确定为什么会这样? 这是我的设置: configureStore.js: import {AsyncStorage,} from 'react-native';import { creat
我一直在尝试使用redux-perist将我的redux-state保存到AsyncStorage.虽然我一直收到错误:
_this.store.getState is not a function 我不确定为什么会这样? 这是我的设置: configureStore.js: import {AsyncStorage,} from 'react-native'; import { createStore,applyMiddleware,compose,combineReducers,} from 'redux'; import reduxThunkMiddleware from 'redux-thunk'; import Reactotron from 'reactotron'; import * as reducers from './modules'; import devTools from 'remote-redux-devtools'; import {persistStore,autoRehydrate} from 'redux-persist' Reactotron.connect({ enabled: __DEV__,}); const enhancer = compose( autoRehydrate(),applyMiddleware( reduxThunkMiddleware,Reactotron.reduxMiddleware,),devTools() ); export default function configureStore(initialState) { const store = createStore( combineReducers({ ...reducers,}),initialState,enhancer,); Reactotron.addReduxStore(store,{storage: AsyncStorage}); return store; } App.js: 这是我将商店连接到我的< provider>的地方: import React from 'react'; import {AsyncStorage} from 'react-native'; import { Provider,connect } from 'react-redux'; import { Router } from 'react-native-router-flux'; import routes from '@routes/app'; import createStore from './redux/create'; // Returns store object from the above configureStore.js! import {persistStore} from 'redux-persist' const RouterWithRedux = connect()(Router); const store = persistStore(createStore(),{storage: AsyncStorage}); // Not working?? const Kernel = () => ( <Provider store={store}> <RouterWithRedux scenes={routes} /> </Provider> ); export default Kernel; 解决方法const RouterWithRedux = connect()(Router); const store = createStore(); const persistor = persistStore(store,{storage: AsyncStorage}); // Not working?? const Kernel = () => ( <Provider store={store} persistor={persistor}> <RouterWithRedux scenes={routes} /> </Provider> ); 问题是我不得不传递一个persistor字段以及store字段. 添加persistor字段后,我的商店被持久化为AsyncStorage 编辑: 这在当时起作用 – 我发现这不是问题的正确解决方案.但我仍然得到它仍然有效的回应,如果有人可以为其他人提供另一个答案,那就太好了. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |