erikras / multireducer

A utility to wrap many copies of a single Redux reducer into a single key-based reducer.
MIT License
422 stars 23 forks source link

Initial action with no type breaks with redux-actions's handleActions() #114

Closed yched closed 7 years ago

yched commented 7 years ago

The reducer generated by plainMultireducer starts by applying an empty action ({}) to the decorated reducers, to let them generate the initial values.

This empty action, having no 'type', is not FSA-compliant, and happens to break with reducers created with redux-actions's handleActions() :

import { createStore } from 'redux'
import { handleActions } from 'redux-actions';
import multireducer from 'multireducer';
const list = handleActions({
  INCREMENT: (state, action) => ({
    counter: state.counter + action.payload
  }),
  DECREMENT: (state, action) => ({
    counter: state.counter - action.payload
  })
}, { counter: 0 });
const reducer = multireducer({
  a: list,
  b: list
});
const store = createStore(reducer);

breaks with "Cannot read property 'toSring' of undefined" when the reducer generated by handleActions() tries to run action.type.toString() when action === {}.

That .toString() call was added in the latest v0.12.0 of react-actions, to support a advanced/edge use case - see https://github.com/acdlite/redux-actions/pull/113. Arguably it's a bit cavalier for an FSA library to internally use "action.type as an object with a toString() method" :-)

Still, using an empty action with no type to bootstrap the reducer seems somewhat wrong ? Why not do like redux does with its '@@redux/INIT' action type ?

yched commented 7 years ago

Cool, thanks for being so quick, @yesmeck