【问题标题】:How to solve: console.error: "redux-persist failed to create sync storage. falling back to "noop" storage如何解决:console.error:“redux-persist 未能创建同步存储。回退到“noop”存储
【发布时间】: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


【解决方案1】:

首先使用下面的行导入 AsyncStorage

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

其次,应为persistConfig 分配如下所示的AsyncStorage,如果您提供了存储对象,则将其注释掉

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

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

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

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

【讨论】:

    【解决方案2】:

    在 redux-persist 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,
    }
    

    【讨论】:

    • 添加异步存储包后不要忘记这样做:cd ios/ && pod install
    • thx... 仍然没有解决这个问题:“”警告:异步存储已从 react-native 核心中提取,并将在未来的版本中删除。现在可以从 '@react-native-community/async-storage' 而不是 'react-native' 安装和导入它。”
    • @user1872384 检查我的详细回答为什么您会收到该错误
    • 别忘了删除import storage from "redux-persist/es/storage"这一行。
    • 值得一提的是,@react-native-community/async-storage 已被弃用并移至名为 @react-native-async-storage/async-storage 的新组织,因此您不妨将 config v6 => 中的 import 语句替换为:import AsyncStorage from '@react-native-async-storage/async-storage';
    【解决方案3】:

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

    确保只导入异步存储

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

    【讨论】:

      【解决方案4】:

      我通过删除来解决它:

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

      并将其替换为:

      import { AsyncStorage } from 'react-native'
      

      不要忘记这一点:

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

      【讨论】:

        【解决方案5】:

        对于 Expo SDK 版本 >=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/

        【讨论】:

        • 您使用的是哪个版本的 Expo?@zengod?您能详细说明错误吗?
        【解决方案6】:

        我遇到了同样的问题,即使我用 AsyncStorage 替换了存储。

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

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

        【讨论】:

          【解决方案7】:

          redux-persist 降级到 5.10.0

          【讨论】:

          • 版本"redux-persist": "^5.10.0"中没有出现错误
          【解决方案8】:

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

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

          它在后台使用的是AsyncStorage,它位于react-native 核心中。

          由于react-native 已弃用AsyncStorage 并将其从react-native 核心中删除,因此redux-persist 的新版本已停止使用它,这似乎是一个不错的决定。

          您现在也可以这样做,但改为从 community version 导入 AsyncStorage

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

          然后在你的配置中使用:

          const persistConfig = {
            storage: AsyncStorage,
            //other configurations
          };
          

          降级到redux-persist v5 不是一个稳定的解决方案,因为它使用核心react-nativereact-native 的AsyncStorage 将在即将发布的版本中完全删除它

          另外,我在 cmets 中读到你不喜欢 AsyncStorage,正如我解释的那样,redux-persist 一直将它用作存储,所以现在唯一的区别是你应该从社区版本而不是从 @ 获取它987654338@核心

          【讨论】:

          • redux-persist v6 + react-native-community/async-storage => 更改后,还不行:(
          • @Mahefa 你遇到了什么问题?
          【解决方案9】:

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

          【讨论】:

          • 是的,您也可以将其降级到低于 6.0.0 的版本
          • 是的,但不建议这样做。在下面查看我的答案为什么会发生这种情况:)
          猜你喜欢
          • 2020-12-13
          • 2021-08-15
          • 1970-01-01
          • 2021-08-29
          • 1970-01-01
          • 2019-01-02
          • 1970-01-01
          • 2019-09-20
          • 2020-05-27
          相关资源
          最近更新 更多