News for hackers. Stuff that matters.
Re:Why-did-you-update – Warnings about unnecessary React upd... by acemarke on Wednesday April 19, 2017 @01:00PM (#14149332)
It's important to note that there's a difference between "handling data immutably" and "using the Immutable.js library". It's completely possible to handle plain JS data immutably _without_ using Immutable.js.

There's a few different reasons why handling data immutably in a React app is a good thing. The main one is that it allows straightforward implementation of `shouldComponentUpdate` by simply doing shallow equality checks against props and state to see if anything has changed. (I wrote a Reddit comment yesterday explaining why that works well, at https://www.reddit.com/r/reactjs/comments/666uin/can_someone... ).

Re:Why-did-you-update – Warnings about unnecessary React upd... by lobster_johnson on Thursday April 20, 2017 @01:29AM (#14154205)
Shallow equality is not a panacea -- it doesn't avoid updates when the content is the same, something which can easily happen.

For example, say you have an action that fetches a bunch of blog posts, then refetches later. If all the items are identical since the last fetch, you want to avoid rendering. So that means that whenever you deal with data updates, de-duplication has to occur at some point, preferably early on in the data layer when the surface area is minimal.

That de-duplication would need to "intern" data; i.e. if you fetch the new blog posts, check each of them if they are exactly the same as what's already cached (time stamps or version numbers could be used to save deep equality checks), and keep the existing object if that's the case.

Of course, you will still end up with lots of identical objects (different identity, but same content) purely because mutations generate them. For example, a user goes to a text field, types a character and then hits backspace. Now you've produced three different objects: version 0 (original data), version 1 (character added), version 2 (identical to v0, different identity). This will correctly re-render each version, of course. But let's say you had some other component (like a preview) that only updated every second. It would receive v0, v1, v2; at this point the delta of v2-v0 is zero, and there's no need to re-render; but a shallow equality check would cause it to. So de-duping needs to be built into all sorts of places you might not anticipate.

Re:Why-did-you-update – Warnings about unnecessary React upd... by wildpeaks on Thursday April 20, 2017 @07:16AM (#14155678)
Deeply frozen data is a good middle ground to have immutability without Immutable.js: during development, automated tests can catch code that would attempt to mutate, and you have the option to remove the deep freezing in the release version to have no runtime penalty once you're confident that nothing can mutate the data.
Re:Why-did-you-update – Warnings about unnecessary React upd... by grovesNL on Wednesday April 19, 2017 @01:25PM (#14149656)
You don't need to write shallow equality checks on each component if you're treating props/state as immutable. You can use React's PureComponent which automatically does this for you: https://facebook.github.io/react/docs/react-api.html#react.p...
Re:Why-did-you-update – Warnings about unnecessary React upd... by acemarke on Wednesday April 19, 2017 @01:51PM (#14149969)
Which does the exact kind of shallow equality checks I was just describing :) The point is that for shallow equality checks to work properly, you do have to handle data immutably - such as using `someArray.concat(item)` instead of `someArray.push(item)`, because otherwise passing in the same references is interpreted as "nothing as changed".