我正在尝试在一个本地应用程序中设置 redux- 持久化。
然而,我遇到了这个错误:
Error: “ redux-keep 创建同步存储失败 回到“ 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)
);
更新:
根据 mychar 的建议解决了错误 github.com/rt2zz/redux-persist/issues/1080:
1) npm install —— save@response-ative- 社区/异步存储
2)在 iOS 上,记得在 iOS 文件夹中执行“ pod install”
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,
}
然而,仍然收到这样的警告: