react-native - 无法保持 react-native 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 库,我也对任何其他解决方案持开放态度。
解决方案
推荐阅读
- python - 如何在django rest框架的ListCreateApiView中使用create函数而不是perform_create
- jenkins - 在 Jenkins Pipeline 的 HTML 正文中使用环境变量
- arrays - 如何在C中初始化数组?
- matrix - 将每一列除以最大值/最后一个值
- eclipse - Apache Tomcat 9.0.40 服务器在 eclipse 上运行时抛出 404 错误。否则它工作正常
- google-cloud-platform - BigQuery 摘要
- wordpress - 如何更改 wordpress 网站上的默认单词?
- java - 分数计算
- rust - 无法将 Struct 属性设置为 Enum Rust
- python - 在 python 中使用 ECB、CBC 和 CFB 模式加密图像时出现错误“IV 没有意义”