coopcycle-js
coopcycle-js copied to clipboard
Use Immutable.js
We should use Immutable.js for the store, to simplify stuff.
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
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
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
Immutable.js is great, but this is adding yet another Facebook library 😐
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.
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)
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?