在我的 react-native 应用程序中,我尝试使用 https://github.com/rt2zz/redux-persist 将我的 redux 状态持久化到本地存储并在应用程序重新启动时将其加载回来。有许多教程与官方 github 页面没有任何不同。我已经尝试了很多次,但是当我关闭应用程序并再次打开时,我仍然会丢失我的数据。这是我的代码的样子:
// store.js
import React from 'react';
import {createStore} from 'redux';
import {persistStore, persistReducer} from 'redux-persist';
import AsyncStorage from '@react-native-community/async-storage';
import rootReducer from '../reducer/index';
const persistConfig = {
key: 'root',
storage: AsyncStorage
};
const persistedReducer = persistReducer(persistConfig, rootReducer);
const store = createStore(persistedReducer);
const persistor = persistStore(store);
export {persistor, store};
// app.js
import {store, persistor} from "./store";
// render method returns
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
// main react-navigation component
</PersistGate>
</Provider>
// reducer/index.js
import {combineReducers} from 'redux'
import language from './language'
const reducers = combineReducers({
language,
...
});
export default reducers;
当我关闭并再次打开应用程序或在命令屏幕上使用 r 刷新复古服务器时,数据会丢失。原因是什么?除了 redux-persist 库,我也愿意接受任何其他解决方案。