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
Provenance
- Commit
955c5f5- Tree
c2782a5- Author
Dean Kerr <oriondean@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
2f0ea7f
2 files changed+65 added−4 removed
modified:website/docs/produce.mdx+3 added-2 removed
7 unmodified lines8 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 lines79 // ...but changed data isn't.80 expect(nextState[1]).not.toBe(baseState[1])81 ```82 83+For advanced TypeScript typing scenarios, see [Using TypeScript or Flow](./typescript.mdx).84+85 ### Terminology86 87 - `(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 linesmodified:website/docs/typescript.mdx+62 added-2 removed
8 unmodified lines9 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 lines62 1. 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.63 2. You can use the utility type `Immutable` to recursively make an entire type tree read-only, e.g.: `type ReadonlyState = Immutable<State>`.64 3. 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.65 66+## Typing reusable recipes67+68+If 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.69+70+```ts71+import {produce} from "immer"72+import type {Draft} from "immer"73+74+type Recipe<S> = (draft: Draft<S>) => void | S75+76+function updateState<S>(77+ setState: (updater: (prev: S) => S) => void,78+ recipe: Recipe<S>79+) {80+ setState(prev => produce(prev, recipe))81+}82+```83+84+### React example85+86+A common usage in React is to wrap `setState` with `produce` so callers can provide a typed recipe.87+88+```tsx89+import {produce} from "immer"90+import type {Draft} from "immer"91+import {useCallback, useState} from "react"92+93+type State = {94+ foo: string95+ bar: string96+}97+98+const initialState: State = {99+ foo: "",100+ bar: ""101+}102+103+type Recipe = (draft: Draft<State>) => void | State104+105+export function Example() {106+ const [state, setState] = useState<State>(initialState)107+108+ const updateState = useCallback((recipe: Recipe) => {109+ setState(prev => produce(prev, recipe))110+ }, [])111+112+ 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+```126+127 ## Tips for curried producers128 129 We 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