我正在尝试在 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 (
<Provider store={store}>
<PersistGate persistor={persistor}>
<Router />
</PersistGate>
</Provider>
);
}
}
在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 <AppIntro />;
}
}
export default withTheme(
connect(
// mapStateToProps,
// mapDispatchToProps
)(Router)
);
Update:
设法根据 mychar 建议解决错误:github.com/rt2zz/redux-persist/issues/1080 https://github.com/rt2zz/redux-persist/issues/1080:
-
npm install --save @react-native-community/async-storage
-
在 iOS 上,记得在 iOS 文件夹中执行“pod install”
-
将存储更改为 AsyncStorage
旧代码 => 从 'redux-persist/lib/storage' 导入存储;
新代码=>从'@react-native-community/async-storage'导入AsyncStorage;
旧代码=>
常量持久配置 = {
//...
贮存,
}
新代码=>
常量持久配置 = {
//...
存储:异步存储,
}
但是,仍然收到此警告: