Skip to content
Myr

current.md

This file

Path

docs/current.md

58 lines · 1.8 KiB · Markdown

Last changed by

docs: updated sponsor linksMichel Weststratebefore adoptionf97d037

History of this file

Take it

Immer 7Michel Weststratebefore adoption467ea5d

lines 1–6why, one step back
1---
2id: current3title: Extracting the current state from a draft4sidebar_label: Current5---6

docs: updated sponsor linksMichel Weststratebefore adoptionf97d037

lines 7–9why, one step back
7<center>
8<div data-ea-publisher="immerjs" data-ea-type="image" class="horizontal bordered"></div>9</center>

Immer 7Michel Weststratebefore adoption467ea5d

lines 10–17why, one step back
10
11Immer exposes a named export `current` that create a copy of the current state of the draft. This can be very useful for debugging purposes (as those objects won't be Proxy objects and not be logged as such). Also, references to `current` can be safely leaked from a produce function. Put differently, `current` provides a snapshot of the current state of a draft.1213Objects generated by `current` work similar to the objects created by produce itself.14151. Unmodified objects will be structurally shared with the original objects.161. If no changes are made to a draft, generally it holds that `original(draft) === current(draft)`, but this is not guaranteed.171. Future changes to the draft won't be reflected in the object produced by `current` (except for references to undraftable objects)

fix: Add missing type `current` for pre-TS 3.7 typesMichel Weststratebefore adoption7d6b57b

line 18why, one step back
181. Unlike `produce` objects created by `current` will _not_ be frozen.

Immer 7Michel Weststratebefore adoption467ea5d

lines 19–58why, one step back
19
20Use `current` sparingly, it can be a potentially expensive operation, especially when using ES5.2122Note that `current` cannot be invoked on objects that aren't drafts.2324### Example2526The following example shows the effect of `current` (and `original`):2728```js29const base = {30 x: 031}3233const next = produce(base, draft => {34 draft.x++35 const orig = original(draft)36 const copy = current(draft)37 console.log(orig.x)38 console.log(copy.x)3940 setTimeout(() => {41 // this will execute after the produce has finised!42 console.log(orig.x)43 console.log(copy.x)44 }, 100)4546 draft.x++47 console.log(draft.x)48})49console.log(next.x)5051// This will print52// 0 (orig.x)53// 1 (copy.x)54// 2 (draft.x)55// 2 (next.x)56// 0 (after timeout, orig.x)57// 1 (after timeout, copy.x)58```