【问题标题】:'state.merge is not a function' in react-nativereact-native 中的“state.merge 不是函数”
【发布时间】:2019-03-12 11:02:34
【问题描述】:

在我的 react-native 应用程序中,如果用户离线,我会使用 redux-persist 来显示商店中存在的数据。但在添加 redux-persist 后,我​​收到错误 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;

提前致谢。

【问题讨论】:

  • 但是你为什么要使用合并? state 似乎没有合并之类的功能!

标签: android react-native redux redux-persist seamless-immutable


【解决方案1】:

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

【讨论】:

  • 谢谢,它可以工作,但需要更多时间来更改根目录
  • 我能做些什么来减少时间
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-27
  • 2015-08-03
  • 1970-01-01
相关资源
最近更新 更多