original.md
This file
Path
docs/original.md
34 lines · 1.1 KiB · Markdown
Last changed by
docs: updated sponsor linksMichel Weststratebefore adoptionf97d037
History of this fileTake it
Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 1–2why, one step back1---2id: originalImmer 7Michel Weststratebefore adoption467ea5d
line 3why, one step back3title: Extracting the original state from a draftMerge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 4–6why, one step back4sidebar_label: Original5---6docs: updated sponsor linksMichel Weststratebefore adoptionf97d037
lines 7–9why, one step back7<center>8<div data-ea-publisher="immerjs" data-ea-type="image" class="horizontal bordered"></div>9</center>Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 10–13why, one step back1011Immer exposes a named export `original` that will get the original object from the proxied instance inside `produce` (or return `undefined` for unproxied values). A good example of when this can be useful is when searching for nodes in a tree-like state using strict equality.1213```jsMerge pull request #493 from scilganon/patch-1Michel Weststratebefore adoption98794b8
line 14why, one step back14import {original, produce} from "immer"Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 15–21why, one step back1516const baseState = {users: [{name: "Richie"}]}17const nextState = produce(baseState, draftState => {18 original(draftState.users) // is === baseState.users19})20```21Immer 7Michel Weststratebefore adoption467ea5d
line 22why, one step back22Just want to know if a value is a proxied instance? Use the `isDraft` function! Note that `original` cannot be invoked on objects that aren't drafts.Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 23–24why, one step back2324```jsMerge pull request #493 from scilganon/patch-1Michel Weststratebefore adoption98794b8
line 25why, one step back25import {isDraft, produce} from "immer"Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 26–34why, one step back2627const baseState = {users: [{name: "Bobby"}]}28const nextState = produce(baseState, draft => {29 isDraft(draft) // => true30 isDraft(draft.users) // => true31 isDraft(draft.users[0]) // => true32})33isDraft(nextState) // => false34```