如何解决state.set不是函数错误?

时间:2018-05-14 02:23:27

标签: javascript reactjs redux immutable.js reducers

这里是我在reducer中使用immutable.js的代码。但每次它给出错误state.set不是一个函数。但是当我用其他方式改变它时(没有不可变)它会改变

import React from 'react';
import _ from 'lodash-node';
import {Map } from 'immutable'
import {ModuleEvents} from './Actions';
const InitialState =  Map({
currentAdmin: null,
data: {},
User:null
});
export const Reducer = (state = InitialState, action) => {
const {
type,
payload
} = action;
switch (type) {
case ModuleEvents.DATA_AVAILABLE:
{
  state = {
    data: payload,
    loading: false,
    User:null
  };
  return state.set('data',payload).set('loading',true)
}

1 个答案:

答案 0 :(得分:1)

state = {
    data: payload,
    loading: false,
    User:null
  };

现在状态不再是一个immutable.js对象,因此它会抛出这样的错误。 尝试,

   case Modulevents.DATA_AVAILABLE: {
      const entry = {}; // INTERNALLY NOT USING IMMUTABLE.JS
      entry['data'] = item;
      entry['loading'] = false;
      entry['User'] = null;
      return state.merge(entry);
    }