0

在我的 react-native 应用程序中,如果用户离线,我将使用 redux-persist 显示商店中存在的数据。但是在添加 redux-persist 之后,我得到了 error state.merge is not a function。我认为这是不变性的错误。请帮我解决这个问题。

rootChangeReducer.js

import * as types from '../actions/login/ActionTypes';
import Immutable from 'seamless-immutable';

const initialState = Immutable({
  root: undefined, // 'login' / 'after-login' / 'register'
});

//root reducer
export function root(state = initialState, action = {}) {

  switch (action.type) {

    case types.ROOT_CHANGED:
      return state.merge({
        root: action.root
      });

    default:
      return state;
  }
}

和 configureStore.js

import { createStore, combineReducers, compose, applyMiddleware } from "redux";
import thunk from "redux-thunk";
import { persistStore, persistCombineReducers } from 'redux-persist';
import storage from 'redux-persist/es/storage';

import masjids from "./reducers/productReducer";
import { root } from "./reducers/rootReducer";


const config = {
  key: 'root',
  storage,
} 

const reducers = {
  root,
  masjids
}

const reducer = persistCombineReducers(config, reducers)

let composeEnhancers = compose;

if (__DEV__) {
  composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
}

const configureStore = () => {
  return createStore(reducer, composeEnhancers(applyMiddleware(thunk)));
};

export default configureStore;

提前致谢。

4

1 回答 1

1

只需简单地使用扩展运算符 (...) 将数据合并或附加到状态,然后返回状态。Like, return {...state,root:action.root} Or 也可以使用中的merge函数lodash

于 2019-03-12T11:21:18.803 回答