Skip to content
This repository has been archived by the owner on Sep 10, 2022. It is now read-only.

withReducer should initialize state using default argument #247

Closed
just-boris opened this issue Sep 21, 2016 · 5 comments · Fixed by #250
Closed

withReducer should initialize state using default argument #247

just-boris opened this issue Sep 21, 2016 · 5 comments · Fixed by #250

Comments

@just-boris
Copy link
Contributor

Usually, in Redux reducers can provide initial state from themselves, using default argument value

const counterReducer = (count = 0, action) => {
  switch (action.type) {
    case INCREMENT:
      return count + 1
    default:
      return count
  }
}

This reducer works within redux store but doesn't work with recompose as expected.

Is there any chance to fix this? For example, call reducer with arbitrary action to get the initial state only when the last argument (initial state) for withReducer is not provided. It will not be breaking for current users, but it will increase reusability of reducers, so you can change them from in-place with recompose to global with redux and the other way round.

@istarkov
Copy link
Contributor

I see the problem, here initial state should be initialized like in redux with action of type https://github.com/reactjs/redux/blob/master/src/createStore.js#L11

So lines above becomes

      state = {
        stateValue: reducer(
          typeof initialState === 'function'
            ? initialState(this.props)
            : initialState,
         { type: ActionTypes.INIT }
        )
      };

@istarkov
Copy link
Contributor

Do you want to provide a PR?

@just-boris
Copy link
Contributor Author

If the basic idea is acceptable, I do.

@istarkov
Copy link
Contributor

Yes, acceptable.

@billyjanitsch
Copy link
Contributor

billyjanitsch commented Dec 13, 2016

FYI, I believe this was done by design. @acdlite has stated several times that if he could co-design redux again, he would not allow reducers to specify an initial state. I assume the asymmetry of this library with respect to redux is due to this belief.

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

Successfully merging a pull request may close this issue.

3 participants