Skip to content
Myr

original.md

This file

Path

docs/original.md

32 lines · 1.0 KiB · Markdown

Last changed by

Immer 7Michel Weststratebefore adoption467ea5d

History of this file

Take it

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 1–2why, one step back
1---
2id: original

Immer 7Michel Weststratebefore adoption467ea5d

line 3why, one step back
3title: Extracting the original state from a draft

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 4–11why, one step back
4sidebar_label: Original
5---67<div id="codefund"><!-- fallback content --></div>89Immer 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.1011```js

Merge pull request #493 from scilganon/patch-1Michel Weststratebefore adoption98794b8

line 12why, one step back
12import {original, produce} from "immer"

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 13–19why, one step back
13
14const baseState = {users: [{name: "Richie"}]}15const nextState = produce(baseState, draftState => {16 original(draftState.users) // is === baseState.users17})18```19

Immer 7Michel Weststratebefore adoption467ea5d

line 20why, one step back
20Just 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 21–22why, one step back
21
22```js

Merge pull request #493 from scilganon/patch-1Michel Weststratebefore adoption98794b8

line 23why, one step back
23import {isDraft, produce} from "immer"

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 24–32why, one step back
24
25const baseState = {users: [{name: "Bobby"}]}26const nextState = produce(baseState, draft => {27 isDraft(draft) // => true28 isDraft(draft.users) // => true29 isDraft(draft.users[0]) // => true30})31isDraft(nextState) // => false32```