Skip to content
Myr

docs: add typed custom produce function (#1239)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* add typed custom produce function * Move TypeScript recipe patterns to TypeScript docs including crosslink

authored by Dean Kerr · committed by GitHub · parent 2f0ea7f · Browse files · 2 files+65 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+65 added−4 removed

  • other:+65 added−4 removed

2 files+65 added−4 removed

modified:website/docs/produce.mdx+3 added-2 removed
7 unmodified lines
8 data-ea-publisher="immerjs"9 data-ea-type="image"10 className="horizontal bordered"11 ></div>12</center>13<details>12</center> <details>13 <summary className="egghead-summary">14 egghead.io lesson 3: Simplifying deep updates with _produce_15 </summary>16 <br />
62 unmodified lines
79// ...but changed data isn't.80expect(nextState[1]).not.toBe(baseState[1])81```8283For advanced TypeScript typing scenarios, see [Using TypeScript or Flow](./typescript.mdx).8485### Terminology8687- `(base)state`, the immutable state passed to `produce`88- `recipe`: the second argument of `produce`, that captures how the base state should be "mutated".
4 unmodified lines
modified:website/docs/typescript.mdx+62 added-2 removed
8 unmodified lines
9 data-ea-publisher="immerjs"10 data-ea-type="image"11 className="horizontal bordered"12 ></div>13</center>14<details>13</center> <details>14 <summary className="egghead-summary">15 egghead.io lesson 12: Immer + TypeScript16 </summary>17 <br />
44 unmodified lines
621. Always define your states as `readonly` as much as possible. This best reflects the mental model and reality, since Immer will freeze all its returned values.632. You can use the utility type `Immutable` to recursively make an entire type tree read-only, e.g.: `type ReadonlyState = Immutable<State>`.643. Immer won't automatically wrap all returned types in `Immutable` if the original type of the input state wasn't immutable. This is to make sure it doesn't break code bases that don't use immutable types.6566## Typing reusable recipes6768If you want to pass a recipe callback around, you can type it using `Draft` and allow either `void` or a replacement state as the return value.6970```ts71import {produce} from "immer"72import type {Draft} from "immer"7374type Recipe<S> = (draft: Draft<S>) => void | S7576function updateState<S>(77 setState: (updater: (prev: S) => S) => void,78 recipe: Recipe<S>79) {80 setState(prev => produce(prev, recipe))81}82```8384### React example8586A common usage in React is to wrap `setState` with `produce` so callers can provide a typed recipe.8788```tsx89import {produce} from "immer"90import type {Draft} from "immer"91import {useCallback, useState} from "react"9293type State = {94 foo: string95 bar: string96}9798const initialState: State = {99 foo: "",100 bar: ""101}102103type Recipe = (draft: Draft<State>) => void | State104105export function Example() {106 const [state, setState] = useState<State>(initialState)107108 const updateState = useCallback((recipe: Recipe) => {109 setState(prev => produce(prev, recipe))110 }, [])111112 return (113 <button114 onClick={() => {115 updateState(draft => {116 draft.foo = "Muffin Man" // this recipe is typed117 })118 }}119 type="button"120 >121 Hello {state.foo}122 </button>123 )124}125```126127## Tips for curried producers128129We try to inference as much as possible. So if a curried producer is created and directly passed to another function, we can infer the type from there. This works well with for example React:130
141 unmodified lines