modifyWithImmer (source code)
- Curried: true
- Failsafe status: not failsafe
The modifyWithImmer
function is designed for immutable modification of values
and is an abstraction over Immer's produce
method. Immer is a library that
simplifies working with complex data structures and state updates in a more
immutable manner.
This function does not include a failsafe mechanism, so it is important to use it with care, especially when working with complex data structures.
You can read more about Immer here.
modifier
: This is a function that receives a draft state as its parameter. The draft state is a mutable version of the original state. Inside the modifier function, we can directly make changes to the draft state as if it were mutable. Once the modifier function finishes, the changes made to the draft state are used to create a new immutable result.state
: The value that needs to be modified.
Returns the new modified value.
const data = { name: "Oliver" };
modifyWithImmer(draft => {
draft.name = "Oliver smith";
})(data);
// outputs: { name: "Oliver smith" }
We can utilize currying with state updator functions:
const { setUsers } = useZustandStore.pick();
setUsers(
modifyWithImmer(draft => {
draft[0].addresses.push("Oliver smith");
})
);
// updates users to: [{ name: "Oliver smith" }, { name: "Eve" }]