web-ridge/react-ridge-state

[idea] add small example using immer

xzilja opened this issue · 1 comments

While out of the scope of this library, I think this could help many people with complex / nested state updates. I'd like to propose addition of one small section to the readme that showcases how to use this library with immer

For example something like this:

cartProductsState.set((prevState) => [
  ...prevState,
  { id: 1, name: "NiceProduct" },
]);

Could become

cartProductsState.set(prev => produce(prev, draft => {
  draft.push({ id: 1, name: "NiceProduct" })
}));

This example doesn't make a dramatic difference, but value of immer can be shown by updating some deeply nested value in an object, or inserting something in the middle of an array.

Let me know what you think and if this is something that can be useful I am down to make a PR 👍

Immer is def really cool, I remember the ambiguity of the spread operator when I started writing reducers a few years back. But now I’m used to it. I think especially new users will like it.

I’m definitely open to a PR, maybe we could add a section ‘Usage with Immer’ and describe what it does with spreading code and the immer code for deeper state (e.g. changing the name of object in an array)