我正在尝试在React Native应用程序中设置redux-persist。
但是我遇到了这个错误:
console.error:“ redux-persist无法创建同步存储。回退到” noop“存储
我尝试将“ src / redux / index.js”中的存储从存储更改为AsyncStorage,但仍然遇到相同的错误:
import AsyncStorage from '@react-native-community/async-storage'; const cOnfig= { key: "root", storage: AsyncStorage // Attempted to fix it (but failed) // storage // old code };
这是其他代码:
在App.js中:
import React, { Component } from "react"; import { Provider } from "react-redux"; import { persistStore } from "redux-persist"; import { PersistGate } from "redux-persist/es/integration/react"; import store from "@store/configureStore"; import Router from "./src/Router"; export default class ReduxWrapper extends Component { render() { const persistor = persistStore(store); return (); } }
在configureStore.js中:
import { applyMiddleware, compose, createStore } from "redux"; import thunk from "redux-thunk"; import reducers from "@redux"; const middleware = [ thunk, // more middleware ]; const cOnfigureStore= () => { let store = null; store = compose(applyMiddleware(...middleware))(createStore)(reducers); return store; }; export default configureStore();
在/src/redux/index.js中
import { persistCombineReducers } from "redux-persist"; import storage from "redux-persist/es/storage"; import { reducer as NetInfoReducer } from "./NetInfoRedux"; import { reducer as UserRedux } from "./UserRedux"; const cOnfig= { key: "root", storage, }; export default persistCombineReducers(config, { netInfo: NetInfoReducer, user: UserRedux, }
在Router.js中
import React from "react"; import NetInfo from "@react-native-community/netinfo/lib/commonjs"; import { Config, AppConfig, Device, Styles, Theme, withTheme } from "@common"; import { AppIntro } from "@components"; import { connect } from "react-redux"; class Router extends React.PureComponent { constructor(props){ super(props) this.state = { loading: true, }; } componentWillMount() { NetInfo.getConnectionInfo().then((connectionInfo) => { this.props.updateConnectionStatus(connectionInfo.type != "none"); this.setState({ loading: false }); }); } render() { return; } } export default withTheme( connect( // mapStateToProps, // mapDispatchToProps )(Router) );
更新:
设法解决基于mychar建议的错误:github.com/rt2zz/redux-persist/issues/1080:
1)npm install-保存@ react-native-community / async-storage
2)在iOS上,请记住在iOS文件夹中执行“ pod安装”
3)将存储更改为AsyncStorage
old code => import storage from 'redux-persist/lib/storage'; new code => import AsyncStorage from '@react-native-community/async-storage'; old code => const persistCOnfig= { //... storage, } new code => const persistCOnfig= { //... storage: AsyncStorage, }
但是,仍然收到此警告:
在redux-persist v6中,您尝试进行如下更改:
旧配置V5 =>
import storage from 'redux-persist/lib/storage'; const persistCOnfig= { //... storage, }
新配置v6 =>
首先添加: yarn add @react-native-community/async-storage
import AsyncStorage from '@react-native-community/async-storage'; const persistCOnfig= { //... storage: AsyncStorage, }
在redux-persist
v6.0.0 之前,您使用的存储方式如下:
import storage from 'redux-persist/lib/storage';
这是什么用途的背景是AsyncStorage
,这是在react-native
核心。
由于react-native
已弃用AsyncStorage
并将其从react-native
核心中删除,因此的新版本redux-persist
已停止使用它,这似乎是一个不错的决定。
您现在可以执行相同的操作,但是可以AsyncStorage
从社区版本导入。
import AsyncStorage from '@react-native-community/async-storage';
然后在您的配置中使用:
const persistCOnfig= { storage: AsyncStorage, //other configurations };
降级到redux-persist
V5并不是一个稳定的解决方案,因为它从核心采用AsyncStorage react-native
和react-native
将彻底删除它在即将到来的版本。
我也读了一些您不喜欢的评论,AsyncStorage
正如我所解释的那样,redux-persist
一直在使用它作为存储,所以现在唯一的区别是您应该从社区版本而不是react-native
核心版本获得它。