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

About initialState #21

Closed
namjul opened this issue Feb 22, 2016 · 7 comments
Closed

About initialState #21

namjul opened this issue Feb 22, 2016 · 7 comments

Comments

@namjul
Copy link

namjul commented Feb 22, 2016

I am wondering why redux-immutable has no default state.

return (inputState, action) => {

Redux combineReducers has a default value. https://github.com/reactjs/redux/blob/6870b826b6b1bea7e7aec67a4ed88d139bd382cd/src/combineReducers.js#L111

I tried to nest redux-immutable combineReducers but it throws Cannot read property 'withMutations' of undefined because I did not specify the nested property in my global initialState Object.

Shouldn't it work without specifying the initialState.

@gajus
Copy link
Owner

gajus commented Feb 22, 2016

What is the problem with explicitly passing Immutable.Map() as the initial value?

@namjul
Copy link
Author

namjul commented Feb 22, 2016

thats fine, just pointing out that without it, it throws an error and because of
reduxjs/redux#1189 (comment)
that maybe it should be possible to not specific an initialstate

@namjul namjul closed this as completed Feb 22, 2016
@lukaskostial
Copy link

Shouldn't there be an option to let reducers (even those dynamically loaded) to define their initial state via default parameter ?

@gajus
Copy link
Owner

gajus commented Feb 22, 2016

Shouldn't there be an option to let reducers (even those dynamically loaded) to define their initial state via default parameter ?

Are you saying something different from what was already said?

@lukaskostial
Copy link

What is the problem with explicitly passing Immutable.Map() as the initial value?

The problem is when you dynamically load a module and want to updateReducers but don't what to update initialState (not sure if it's even possible but I'm not that familiar with redux yet). Plus your dynamically-loaded reducer is also produced by redux-immutable combineReducers.

@gajus
Copy link
Owner

gajus commented Feb 22, 2016

I am just not following you, at all. For me to further assist with this issue, I'd need a code example.

@lukaskostial
Copy link

I'm sorry I didn't make myself clear. What I meant is below:

import { createStore } from 'redux'
import { combineReducers } from 'redux-immutable'
import { Map } from 'immutable'

const reducerA = (state = "A", action) => state
const reducerB = (state = "B", action) => state

const createReducer = (additionalReducers) => {
    return combineReducers({
      ...additionalReducers,
      a: reducerA,
      b: reducerB
    })
}
// Create store with empty initial state
const store = createStore(createReducer(), Map({}))
console.log(store.getState().toJS())

// Then dynamically load another module with reducerC (e.g. via SystemJS System.import)

const reducerC = (state = "C", action) => state
const reducerD = (state = "D", action) => state

const newModuleReducer = combineReducers({
  c: reducerC,
  d: reducerD
})

// Add the loaded reducer to the store. The store structure should be:
// {
//  a: "A",
//  b: "B",
//  inner: { c: "C", d: "D" }
// }
store.replaceReducer(createReducer({inner: newModuleReducer}))

console.log(store.getState().toJS())

This works OK with standard redux.

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

3 participants