Skip to content
Myr

current.md

This file

Path

website/docs/current.md

58 lines · 1.8 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 b7fcf7f point at it.

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

lines 1–7why, one step back
1---
2id: current3title: Extracting the current state from a draft4sidebar_label: Current5---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>

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

lines 9–40why, one step back
9</center>
1011Immer exposes a named export `current` that creates 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)181. Unlike `produce` objects created by `current` will _not_ be frozen.1920Use `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(() => {

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

line 41why, one step back
41 // this will execute after the produce has finished!

docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f

lines 42–58why, one step back
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```