Skip to content
Myr

original.md

This file

Path

website/docs/original.md

34 lines · 1.1 KiB · Markdown

Last changed by

docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95

History of this file

Take it

Show the source

original.md


id: original title: Extracting the original state from a draft sidebar_label: Original

Immer 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.

import {original, produce} from "immer"

const baseState = {users: [{name: "Richie"}]}
const nextState = produce(baseState, draftState => {
	original(draftState.users) // is === baseState.users
})

Just 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.

import {isDraft, produce} from "immer"

const baseState = {users: [{name: "Bobby"}]}
const nextState = produce(baseState, draft => {
	isDraft(draft) // => true
	isDraft(draft.users) // => true
	isDraft(draft.users[0]) // => true
})
isDraft(nextState) // => false