web-dev-qa-db-fra.com

Comment résoudre: console.error: "redux-persist n'a pas réussi à créer un stockage de synchronisation. Retombant sur le stockage" noop "

J'essaie de configurer redux-persist dans une application native réagissant.

Cependant, je frappe cette erreur:

console.error: "redux-persist n'a pas réussi à créer un stockage de synchronisation. Revenir au stockage" noop "

J'ai essayé de changer le stockage du stockage en AsyncStorage dans "src/redux/index.js", mais il rencontre toujours la même erreur:

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

const config = {
  key: "root",
  storage: AsyncStorage // Attempted to fix it (but failed)
  // storage // old code
};

Voici les autres codes:

Dans 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>
    );
  }
}

Dans 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();

Dans /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,
}

Dans 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)
);

Mettre à jour:

Géré pour résoudre la base d'erreur sur la suggestion de mychar: github.com/rt2zz/redux-persist/issues/1080:

1) npm install --save @ react-native-community/async-storage

2) sur iOS, n'oubliez pas d'effectuer une "installation de pod" dans le dossier iOS

3) Changez le stockage en 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,
}

Cependant, vous obtenez toujours cet avertissement:

enter image description here

28
user1872384

Je le résous en supprimant:

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

et en le remplaçant par:

import { AsyncStorage } from 'react-native'

N'oubliez pas ceci:

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