Reputation: 387
I'm trying to use redux with immutable js. I made two reducers (layout, task) and it's working fine.
layout.js
const initialState = Map({
width: Dimensions.get('window').width,
height: Dimensions.get('window').height,
});
export default function layout(state = initialState,action){
// Some Tasks
}
task.js
const initialState = Map({
list: Map({
tasks: List([]),
status: 'INIT',
})
});
export default function task(state = initialState,action){
// Some Tasks
}
My problem is when i try to get state by mapStateToProps like
const mapStateToProps = (state) => {
return {
task: state.get('task')
}
}
it gives me an error 'state.get is not a function'. when i changed it to
task: state.task
it works fine.
My thought is it's because combineReducers doesn't make immutable state.
Any solution for this? Or using task: state.task
is the right way?
Upvotes: 0
Views: 52
Reputation: 183
You will have to use the redux-immutable
library to make your store also an immutable object. By default it would return an object
.
There are more details here:
When
createStore
is invoked withinitialState
that is an instance ofImmutable.Collection
further invocation of reducer will produce an error:"The initialState argument passed to createStore has unexpected type of "Object". Expected argument to be an object with the following keys: "data""
This is because Redux
combineReducers
treats state object as a plain JavaScript object.
combineReducers
created using redux-immutable uses Immutable.js API to iterate the state.
.
Upvotes: 1