produce
Marks a draft updater.
Marks a draft updater. It is the function itself (setStore(produce((d) => { d.n += 1 })) is
setStore((d) => { d.n += 1 })); it exists so code written for other libraries migrates as is.
Import#
ts
import { produce } from "@shadow-js/core";
Signature#
ts
function produce<T>(update: (draft: T) => void): (draft: T) => void
Parameters#
| Name | Description |
|---|---|
update |
The draft updater: it mutates the draft it receives. |
Returns#
update, unchanged.
Examples#
ts
import { produce, useStore } from "@shadow-js/core";
const [cart, setCart] = useStore({ items: [] as string[], count: 0 });
setCart(
produce((draft) => {
draft.items.push("apple");
draft.count += 1;
}),
);
console.log(cart.count); // 1