current.md
This file
Path
website/docs/current.md
58 lines · 1.8 KiB · Markdown
Last changed by
docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95
History of this fileTake it
docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95
lines 1–58why, one step back1---2id: current3title: Extracting the current state from a draft4sidebar_label: Current5---67<center>8<div data-ea-publisher="immerjs" data-ea-type="image" class="horizontal bordered"></div>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(() => {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```