Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrapping reducer in handleAction throws undefined state #23

Closed
Bartvds opened this issue Aug 29, 2015 · 16 comments
Closed

Wrapping reducer in handleAction throws undefined state #23

Bartvds opened this issue Aug 29, 2015 · 16 comments

Comments

@Bartvds
Copy link

Bartvds commented Aug 29, 2015

If I wrap a reducer in handleAction I get this error:

"Reducer "selectedReddit" returned undefined during initialization. If the state passed to the reducer is undefined, you must explicitly return the initial state. The initial state may not be undefined."

If I debug I see redux tries to get a initial state by calling the wrapped reducer with undefined state and a specific system action type. Because the type does not match the wrapped one the inner reducer never runs and no state default is created.

If I use handleActions (m) with default state it works fine. Same function with default state in arguments and classic action-type check is also fine.

Anyway,is handleAction usable like this? Readme doesn't really specifiy use case.

@iest
Copy link

iest commented Sep 11, 2015

Got this error too. Using handleActions instead and passing a default state worked for me as well.

@yoavniran
Copy link

yup, running into this as well. Seems like handleAction is broken with initial state being undefined...

@drhayes
Copy link

drhayes commented Oct 22, 2015

Happening for me too. Same workaround.

I'm getting it because I'm passing the resultant reducer to combineReducers from redux. It aggressively "ensures reducer sanity" by passing undefined initially. Third arg, maybe?

@tony-kerz
Copy link

i get this:

Uncaught Error: Reducer "loginReducer" returned undefined during initialization. If the state passed to the reducer is undefined, you must explicitly return the initial state. The initial state may not be undefined.

from this:

export const loginReducer = handleAction(
  actions.LOGIN,
  (state={}, action) => {
    return {
      ...state,
      session: {
        active: true
      }
    }
  }
)

this is with redux-actions 0.8.0 (alongside of redux-promise 0.6.0-alpha if it matters)

i'm assuming this is the same issue...

@talarari
Copy link

Getting the same error , should be possible to do this:
const connectionState = handleAction("connect", (state= 'disconnected',action)=> 'connecting')
const rootReducer = combineReducers({
connectionState
})

@apisurfer
Copy link

Same problem here when I define reducers with handleAction and then try to combine them with combineReducers from redux. They go through internal combineReducers sanity check that calls them without passing an state or action an that crashes the function

@saiichihashimoto
Copy link

Same

@danny-andrews
Copy link
Contributor

+1, this function is unhelpful as it stands.

@emiloberg
Copy link

For others finding their way in here:

Note that handleActions (plural) works but wants the default state as the second argument to handleActions. As can be seen in the readme or in this example

const initState = {};
const aReducer = handleActions({
    SOMETHING: (state, action) => ({
        //do stuff
    })
}, initState);

const reducers = combineReducers({
    aReducer
});


@lsunsi
Copy link

lsunsi commented Mar 4, 2016

Same problem. So handleAction is practically unusable, right?

@timche
Copy link
Member

timche commented May 15, 2016

I don't know since when Redux is doing this sanity check, but without an initial state, handleAction is definitely unusable.

Nevertheless, #55 adds support for that.

@evenfrost
Copy link

Looking forward to this as well, going through the Redux tutorial and handleAction bundled with combineReducers throws the error.

@yangmillstheory
Copy link
Contributor

@timche isn't this closed with 0.10.0?

@timche
Copy link
Member

timche commented Jun 21, 2016

Thanks for the reminder @yangmillstheory 👍

@vtvz
Copy link

vtvz commented Apr 27, 2017

Solution:

const someReducer = handleAction(someAction, (state, {payload}) => {
    return {
        ...state,
        ...payload,
    };
}, {}); // <-- default state

The reason why this should be like this. Third param in handleAction should not be empty.

@KalidassM01
Copy link

You have to add default: return state; at the bottom of all cases of your switch function. Hope this work. Good luck

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests