Skip to content
Myr

original.md

This file

Path

website/docs/original.md

34 lines · 1.1 KiB · Markdown

Last changed by

chore: docusaurus upgradeMichel Weststratebefore adoption48a91b3

History of this file

Take it

The walk stopped after 200 commits: lines older than 07f5cc9 point at it.

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 1–7why, one step back
1---
2id: original3title: Extracting the original state from a draft4sidebar_label: Original5---67<center>

chore: docusaurus upgradeMichel Weststratebefore adoption48a91b3

line 8why, one step back
8<div data-ea-publisher="immerjs" data-ea-type="image" className="horizontal bordered"></div>

chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9

lines 9–34why, one step back
9</center>
1011Immer 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```js14import {original, produce} from "immer"1516const baseState = {users: [{name: "Richie"}]}17const nextState = produce(baseState, draftState => {18 original(draftState.users) // is === baseState.users19})20```2122Just 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.2324```js25import {isDraft, produce} from "immer"2627const 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```