Redux-persist v5保存了部分状态但不保存其他部分

时间:2018-03-23 21:36:48

标签: javascript reactjs react-native redux redux-persist

所以我使用React Native和redux以及redux-persist并使用AsyncStorage存储我的数据。我的商店看起来像这样:

import { createStore, applyMiddleware } from "redux";
import { AsyncStorage } from "react-native";
import { persistStore, persistReducer } from "redux-persist";
import ReduxThunk from "redux-thunk";
import reducers from "./reducers";
import autoMergeLevel2 from "redux-persist/lib/stateReconciler/autoMergeLevel2";

const persistConfig = {
  key: "root",
  storage: AsyncStorage,
  stateReconciler: autoMergeLevel2,
  blacklist: ["prices"]
};

const persistentReducer = persistReducer(persistConfig, reducers);

export const store = createStore(
  persistentReducer,
  {},
  applyMiddleware(ReduxThunk)
);
export const persistor = persistStore(store);

我的减速器看起来像这样:

import { combineReducers } from "redux";
import AuthReducer from "./AuthReducer";
import PriceReducers from "./PriceReducers";
import SearchReducers from "./SearchReducers";
import SetPriceReducers from "./SetPriceReducers";
import GetCatalogReducers from "./GetCatalogReducers";

export default combineReducers({
  auth: AuthReducer,
  prices: PriceReducers,
  search: SearchReducers,
  setPrice: SetPriceReducers,
  catalog: GetCatalogReducers
});

我的App.js看起来像这样:

import React, { Component } from "react";
import { SafeAreaView } from "react-native";
import { Provider } from "react-redux";
import { store, persistor } from "./src/store";
import Router from "./src/Router";
import { PersistGate } from "redux-persist/lib/integration/react";
import { Loading } from "./src/components/common";

class App extends Component {
  render() {
    console.log(store.getState().catalog);
    return (
      <SafeAreaView style={styles.safeArea}>
        <Provider store={store}>
          <PersistGate loading={<Loading />} persistor={persistor}>
            <Router />
          </PersistGate>
        </Provider>
      </SafeAreaView>
    );
  }
}
.....

最后,这是我的“目录”缩减器:

import { GET_CATALOG } from "../actions/types";

const INITIAL_STATE = {};

export default (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case GET_CATALOG:
      console.log("reducer", action.payload);
      return { ...action.payload };
    default:
      return { ...state };
  }
};

主要问题是我的“身份验证”状态会持续存在,但状态的其他部分(不包括列入黑名单的“价格”)不会持续存在。 我知道我的州包含了这些信息,因为我可以console.log(store.getState().catalog) 并查看所有数据,但出于某种原因,当我刷新应用程序时,状态的“目录”部分为空。而且我的“auth”部分一直都很完美。

任何可能导致我国某些部分持续存在的想法而不是其他人的想法?

这篇文章(https://blog.reactnativecoach.com/the-definitive-guide-to-redux-persist-84738167975)和redux-persist文档是我用来设置redux-persist的。

0 个答案:

没有答案