如何解决: console. error: “ redux-keep 创建同步存储失败。退回到“ noop”存储

我正在尝试在一个本地应用程序中设置 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,
}

然而,仍然收到这样的警告:

enter image description here

44548 次浏览

通过将 redux-persis 版本降级到“5.10.0”,我的错误得到了解决。

在 redux 持久化 v6中,尝试进行以下更改:

旧配置 V5 = >

import storage from 'redux-persist/lib/storage';
const persistConfig = {
//...
storage,
}

新配置 v6 = >

第一条补充: yarn add @react-native-async-storage/async-storage

import AsyncStorage from '@react-native-async-storage/async-storage';
const persistConfig = {
//...
storage: AsyncStorage,
}

redux-persist v6.0.0之前,您是这样使用存储的:

import storage from 'redux-persist/lib/storage';

这在后台使用的是 react-native核心中的 AsyncStorage

由于 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不是一个稳定的解决方案 ,因为它在即将发布的版本中使用来自核心 react-nativereact-native 会完全移除的 AsyncStorage。

我还看到一些评论说你不喜欢 AsyncStorage,我解释说 redux-persist一直把它作为存储器使用,所以现在唯一的区别是你应该从社区版本而不是从 react-native核心获得它

redux-persist下调至5.10.0

即使我用 AsyncStorage 替换了存储,我也面临同样的问题。

我通过删除原始存储的导入线解决了这个问题

import storage from "redux-persist/es/storage"; //remove this

对于 博览会 SDK version > = 33 Workflow,插件 @react-native-community/async-storage不起作用。

这招对我很管用。

import { AsyncStorage } from 'react-native';`
.
.
.
const persistConfig = {
key: 'root',
storage: AsyncStorage
}

参考资料: https://docs.expo.io/versions/latest/react-native/asyncstorage/

我通过移除:

import storage from 'redux-persist/lib/storage'

取而代之的是:

import { AsyncStorage } from 'react-native'

别忘了:

const rootPersistConfig = {
key: 'root',
storage: AsyncStorage
}

在导入 import storage from 'redux-persist/lib/storage'中注释掉/排除这一行

确保只导入异步存储

import AsyncStorage from '@react-native-community/async-storage';

首先,使用以下代码行导入 AsyncStorage

import AsyncStorage from '@react-native-community/async-storage';

其次,应该像下面这样分配恒久配置 AsyncStorage,如果提供了存储对象,则将其注释掉

const persistConfig = {
// storage,
storage: AsyncStorage,
}

最后一行导入存储或任何其他存储 不应该存在在您的文件

// import storage from 'redux-persist/lib/storage'  COMMENT THIS OUT

这就是我解决问题的方法。

注释//从“ reduc- 持久/库/存储”导入存储;

并从“@response-national-community/sync-Storage”导入 AsyncStorage;