Skip to content
Myr

Add small section explaining how to use "void" for inline mutations

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

authored by Paul Henschel · committed by GitHub · parent 2897e12 · Browse files · 1 file+14 added−0 removed
This commit

State

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

Diff stats

1 file+14 added−0 removed

  • other:+14 added−0 removed

1 file+14 added−0 removed

modified:readme.md+14 added
370 unmodified lines
371})372console.log(increment(base).counter) // 1373```374375## Inline shortcuts using `void`376377Draft mutations in Immer usually warrant a code block, since a return denotes an overwrite. Sometimes that can stretch code a little more than you might be comfortable with. 378379In such cases you can use javascripts [`void`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void) operator, which evaluates expressions and returns `undefined`.380381```javascript382// Single mutation383produce(draft => void (draft.user.age += 1))384385// Multiple mutations386produce(draft => void (draft.user.age += 1, draft.user.height = 186))387```388389## TypeScript or Flow390391The Immer package ships with type definitions inside the package, which should be picked up by TypeScript and Flow out of the box and without further configuration.392
218 unmodified lines