Skip to content
Myr

current.md

This file

Path

website/i18n/zh-CN/docusaurus-plugin-content-docs/current/current.md

58 lines · 1.7 KiB · Markdown

Last changed by

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

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–10why, one step back
1---
2id: current3title: 从 draft 中提取当前 state4sidebar_label: Current5---67<center>8<div data-ea-publisher="immerjs" data-ea-type="image" class="horizontal bordered"></div>9</center>10

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

line 11why, one step back
11Immer 暴露了一个命名导出的 `current`函数,可以创建 draft 对象当前状态的一个副本。这对于调试非常有用(因为这些对象不会是代理对象,也不会被记录下来)。此外,对 `current` 的引用可以安全地从 `produce` 函数中释放。换句话说,`current` 提供 draft 当前状态的快照。

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

lines 12–58why, one step back
12
13`current` 工作生成的对象类似于 `produced` 本身创建的对象。14151. 未修改的对象将在结构上与原始对象共享。162. 如果未对 draft 进行任何更改,通常它会保留 original(draft) === current(draft),但这并不能保证。173. 未来对 draft 的更改不会反映在 `current` 生成的对象中(不可被 draft 对象的引用除外)184. 与 `produce` 创建的对象不同,`current` 创建的对象不会被冻结。1920谨慎使用 `current`,这可能是一项潜在的昂贵操作,尤其是在使用 ES5 时。2122请注意,不能在不是 draft 的对象上调用 `current`。2324### 例子2526以下示例显示了 `current`(和 `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 // 将在 produce 完成后执行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// 将会打印52// 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```