Skip to content
This repository has been archived by the owner on Oct 9, 2019. It is now read-only.

Use Immutable.js #2

Open
alexsegura opened this issue Jul 25, 2017 · 7 comments
Open

Use Immutable.js #2

alexsegura opened this issue Jul 25, 2017 · 7 comments

Comments

@alexsegura
Copy link
Member

We should use Immutable.js for the store, to simplify stuff.

@mastilver
Copy link

Hi @alexsegura

What about using: https://github.com/leoasis/redux-immutable-state-invariant?
You wouldn't need to use an extra lib and you could still use native JS

@alexsegura
Copy link
Member Author

Thank you for your comment.

It says "For development use only".

The goal to use Immutable.js is to avoid having to clone arrays for example

@mastilver
Copy link

mastilver commented Aug 1, 2017

Yes you are right, you would still need to clone your array.

You should have a look at http://redux.js.org/docs/recipes/reducers/NormalizingStateShape.html to prevent that

@alexsegura
Copy link
Member Author

Immutable.js is great, but this is adding yet another Facebook library 😐

@tom-s
Copy link

tom-s commented Aug 10, 2017

Hi guys, great project. I had a quick look at the reducers and I don't think there is any need for cloning or deep cloning objects. One usual way to do it is to store objects by their ids (using normalizr for instance and as suggested by @mastilver in the link above) and then it's easy to update a given id in the reducer (es6 spread ... helps to keep things readable). Immutable could be the next step (i think it's a nice addition even though my experience tells me that well written reducer and reducers tests are usually enough to prevent mutating), but I think the reducers need to be reworked a bit before adding immutable. I hope it helps, I'd be happy to assist you or submit a pull request if that's of any interest to you.

@tom-s
Copy link

tom-s commented Aug 10, 2017

for instance
case 'CREATE_ADDRESS_SUCCESS': user = _.cloneDeep(state); user.addresses.push(action.address);

could become

case 'CREATE_ADDRESS_SUCCESS': return { ...state, addresses: [ ...state.addresses, action.address ] }

It guaranties immutability because every action returns a new state (same goes for your "substates" like adresses)

@alexsegura
Copy link
Member Author

Hi @tom-s, many thanks for your kind comments.

Actually I confess this is the 2nd time in my life I'm using Redux, and the 1st time we used Immutable.js 😅

You can definitely submit a Pull Request! We would really be interested in reducers tests.
Do you use Jest for testing?

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

No branches or pull requests

3 participants