【发布时间】:2020-01-06 22:36:36
【问题描述】:
我正在尝试在 react 本机应用程序中设置 redux-persist。
但是我遇到了这个错误:
console.error: "redux-persist 未能创建同步存储。下降 返回“noop”存储
我尝试在“src/redux/index.js”中将存储从 storage 更改为 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 @react-native-community/async-storage
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,
}
但是,仍然收到此警告:
【问题讨论】:
-
你的 react-native 版本是什么?你链接了@react-native-community/async-storage
-
0.60.5... 不,我不喜欢异步存储
-
@mychar,非常感谢。上述错误消失了,但是我仍然遇到警告,你知道如何摆脱这个警告吗? “警告:异步存储已从 react-native 核心中提取,并将在未来版本中删除。现在可以从“@react-native-community/async-storage”而不是“react-native”安装和导入。见github.com/react-native-community/react-native-async-storage"
-
@user1872384,如何消除控制台错误。仍然,我收到控制台错误
标签: javascript react-native redux-persist