-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
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
How to initialize redux with immutable object #153
Comments
I think what you want is something like this // stores/some-store.js
import Immutable from 'immutable'
const { Map, List, fromJS } = Immutable
const initialState = Map({
foo: 'bar',
fooList: List()
})
export function someStore (state = initialState, action) {
if (action.type === 'FOO') {
return state.set('foo', fromJS(action.foo))
}
if (action.type === 'FOO_LIST') {
return state.set('fooList', fromJS(action.fooList))
}
return state
}
// stores/index.js
export { default as someStore } from './some-store'
// application.js
import * as stores from './stores'
const redux = createRedux(stores) |
You should let stores define their own initial state using default parameter like @emmenko showed. The That said, what you tried should work. Can somebody investigate? |
No, what I tried does not work. I just want to create initial data for all stores in one place. I guess this is what I shouldn't do. |
It's up to you. I find it better to divide the initial data. Still, if something doesn't work, let's keep it open? I'll revisit next week and see why it did not work. |
The problem is with There may be other places where it's assuming a plain object though. |
Oh. That's a great catch! Thanks for explaining. This makes perfect sense. |
Using connectors also fails for the very same reason: |
@pierregm This is actually correct. If it spread the Record properties over the component props, that wouldn't work anyway. It is up to you to write |
I was able to get it to work like this, // store
const initialState = Immutable.fromJS({}).toOrderedMap();
export default function streamStore(state = initialState, action) {
switch (action.type) {
case actionTypes.FETCH_STREAM_DONE:
var streamMap = {};
for (var i in action.stream_data) {
var item = action.stream_data[i];
streamMap[item.id] = item;
}
return state.merge(Immutable.fromJS(streamMap).toOrderedMap());
default:
return state
}
};
// select
function select(state) {
return {stream: state.stream.toList()}
} It works perfectly but it ends up re-rendering all items in the stream (a list view). I was wondering how one could implement something that compares the old state vs new state at store level and invalidates the need to render any components right there when there are no changes to the data. NuclearJS does this with Immutable structures. What would the recommended way be to implement something like that in redux? |
Check out Reselect: https://github.com/faassen/reselect. It provides functionality similar to NuclearJS getters. |
@gaearon Cool! Great talk at ReactEurope BTW! Thanks. |
This is my attempt to use Immutable.js with redux 1.0.0, https://github.com/gajus/redux-immutable |
Nice @gajus! Testing out |
Thank you @chiplay. I would really appreciate feedback should you come across deficiencies or have ideas for improvement. |
is there any advantage of using immutable structures together with redux? when the redux is based on immutable behaviour (eg. stores return new copy of state)? Performance (any benchmarks?)? or something else? thx |
Pros of using Immutable with Redux:
Cons of using Immutable with Redux:
The tradeoff is up to you! |
@gaearon |
If you want ImmutableJs & Redux that conforms Redux standards you can take a look at https://github.com/indexiatech/redux-immutablejs, @gaearon Is it possible to put a link on the web site ? |
Thank you Asaf. I am sure people will find it useful.
|
Good stuff, would you like to make a PR to |
Hi all, Basic use case : getting a collection of data from an API call and storing it in a reducer... From my reducer, const initialStore = {
isLoading : false,
error : null,
data : []
}
// in the switch case
return {
...state,
isLoading: false,
error: null,
data: action.result
} With ImmutableJS const initialStore = Map({
isLoading : false,
error : null,
data : List()
})
// in the handler function
return state.merge({
isLoading: false,
error: null,
data: List(result.result)
}) I am wondering if the "treatment" ImmutableJS is doing isn't costing more here ? From what I understand from Immutability, the benefit is more when the rendering quicks in, right ? Thanks for your feedback. |
The benefit is that operating on large arrays and objects costs less memory and works faster. It doesn't make a large difference in small applications. |
Is it possible or I'm doing smt wrong?
The text was updated successfully, but these errors were encountered: