如何解决无法持久反应原生 redux 状态
在我的 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 库,我也愿意接受任何其他解决方案。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。