Expo React Native App + Redux-Persist:AsyncStorage 问题

Nyx*_*nyx 4 reactjs react-native asyncstorage expo redux-persist

我刚刚启动了一个新的托管Expo React Native 项目(Expo SDK 35、 React Native 0.5.9redux-persist@6.0.0,但即使是带有 redux/redux-persist 的新 expo 项目当前也会抛出以下错误。


使用AsyncStorage来自react-native,

我们得到错误:

redux-persist:需要 config.storage。尝试使用提供的存储引擎之一import storage from 'redux-persist/lib/storage'


使用AsyncStorage来自@react-native-community/async-storage,

我们得到错误:

[@RNC/AsyncStorage]:NativeModule:AsyncStorage 为空。


使用storage来自redux-persist/lib/storage,

我们得到错误:

console.error:“redux-persist 无法创建同步存储。回退到 noop 存储。”


问:如何解决这个问题而不弹出?谢谢!


redux-persist代码

注意:之前的尝试已被注释掉:

// Chose 1 of the 3 storages
// import { AsyncStorage } from "react-native";
// import AsyncStorage from '@react-native-community/async-storage';
import storage from 'redux-persist/lib/storage'

import { createMigrate, persistStore, persistReducer } from "redux-persist";
import reducer from "../reducers";

const persistConfig = {
    key: 'root',
    version: 0,
    storage,    // 'redux-persist/lib/storage'
}

// const persistConfig = {
//   key: 'root',
//   version: 0,
//   AsyncStorage,    // '@react-native-community/async-storage'
// }

const persistedReducer = persistReducer(persistConfig, reducer);
Run Code Online (Sandbox Code Playgroud)

tol*_*tra 7

此导入适用于 Web 应用程序:

import storage from 'redux-persist/lib/storage'
Run Code Online (Sandbox Code Playgroud)

对于 React Native,您应该使用以下导入:

import AsyncStorage from '@react-native-community/async-storage';
import { createMigrate, persistStore, persistReducer } from "redux-persist";
import reducer from "../reducers";

const persistConfig = {
    key: 'root',
    version: 0,
    //...
    storage: AsyncStorage
}

const persistedReducer = persistReducer(persistConfig, reducer);
Run Code Online (Sandbox Code Playgroud)


Aro*_*sha 6

对于 React Native,下面的代码不起作用

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

const persistConfig = {
    key: 'root',
    AsyncStorage
}
Run Code Online (Sandbox Code Playgroud)

因为persistConfig需要一把钥匙storage

下面persistConfig 适用于 React Native。

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

const persistConfig = {
    key: 'root',
    storage: AsyncStorage
}
Run Code Online (Sandbox Code Playgroud)