如何将CombineReducers与handleActions一起使用

时间:2019-08-23 14:46:23

标签: javascript redux immutable.js redux-actions redux-immutable

我正在尝试创建新的减速器survey,在使用name包中的questions时,它必须结合handleActionsredux-actions减速器。但是我收到一个错误Invariant Violation: Expected handlers to be a plain object.,该如何更改它才能正常工作?

import { Map, List } from 'immutable';
import { combineReducers } from 'redux-immutable';
import { handleActions } from 'redux-actions';

const initialNameState = List();
const initialQuestionsState = List();

const name = handleActions({}, initialNameState);
const questions = handleActions({}, initialQuestionsState);

export const initialSurveyState = Map({
  name: initialNameState,
  questions: initialQuestionsState
});

export const survey = handleActions(
  combineReducers({
    name,
    questions
  }),
  initialSurveyState
);

2 个答案:

答案 0 :(得分:1)

handleAction实际上创建了一个减速器。直接使用combineReducer应该可以解决问题。

import React, { Component } from 'react';
import { render } from 'react-dom';
import { createStore, combineReducers } from 'redux';
import { connect, Provider } from 'react-redux';
import { Map, List } from 'immutable';
import { combineReducers } from 'redux-immutable';
import { handleActions, createAction, combineActions } from 'redux-actions';

const initialNameState = List();
const initialQuestionsState = List();

const name = handleActions({}, initialNameState);
const questions = handleActions({}, initialQuestionsState);

export const survey = combineReducers({name, questions});

const store = createStore(survey);

答案 1 :(得分:1)

尝试一下,告诉我是否可行

import { createStore, combineReducers } from 'redux';

const initialNameState = List();
const initialQuestionsState = List();

const name = handleActions({}, initialNameState);
const questions = handleActions({}, initialQuestionsState);

 const initialSurveyState = Map({
  name: initialNameState,
  questions: initialQuestionsState
});
    export const rootReducer=combineReducers({
    name,
    questions
  })

  const store=createStore(rootReducer)