Interesting, thanks for sharing. But this looks like the default useState way to deal with arrays, doesn't it? (With just your example, I don't really understand the benefits of using it.)
I agree it's not a very good example for showing the benefits of Immer, but basically it just allows you to write "mutating" syntax while not actually mutating the data. Try recreating my toggleCompleted function with useState and you'll see the difference.
Okay now I see the benefits, and I totally agree with you! Never had to deal with such complex examples though, but it's great to have this library in mind. 🙌
But I can see this saving time again and again just due to its simplified mental model.
Writing nested spread after nested spread gets a little complicated after a while. Now I'm so far down the pattern that when I see syntax looking like it's mutating a variable out of scope I balk a little. All the same, it would be a nice tool.
Interesting, thanks for sharing. But this looks like the default
useState
way to deal with arrays, doesn't it? (With just your example, I don't really understand the benefits of using it.)I agree it's not a very good example for showing the benefits of Immer, but basically it just allows you to write "mutating" syntax while not actually mutating the data. Try recreating my
toggleCompleted
function with useState and you'll see the difference.Take this slightly more complex data structure:
If you wanted to update the quantity of a stock item with
useState
you would maybe do something like this:With
useImmer
you can instead do this:Might not be a big deal, but for me the latter takes less mental effort.
codesandbox.io/s/heuristic-wiles-p...
Okay now I see the benefits, and I totally agree with you! Never had to deal with such complex examples though, but it's great to have this library in mind. 🙌
But I can see this saving time again and again just due to its simplified mental model.
Writing nested spread after nested spread gets a little complicated after a while. Now I'm so far down the pattern that when I see syntax looking like it's mutating a variable out of scope I balk a little. All the same, it would be a nice tool.
Yeah, it definitely feels dirty writing it that way at first but it's a small price to pay for getting away from some nested spread abominations.