Skip to content
Myr

async.mdx

This file

Path

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

56 lines · 1.8 KiB

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–2why, one step back
1---
2id: async

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 3–4why, one step back
3title: createDraft / finishDraft
4sidebar_label: createDraft / finishDraft

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

lines 5–12why, one step back
5---
67<center>8 <div9 data-ea-publisher="immerjs"10 data-ea-type="image"11 className="horizontal bordered"12 ></div>

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

lines 13–14why, one step back
13</center>
14<details>

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

lines 15–38why, one step back
15 <summary className="egghead-summary">
16 egghead.io 第11课: 创建<b>异步</b> producers(以及为什么不应该这样做)17 </summary>18 <br />19 <div>20 <iframe21 width="760"22 height="427"23 scrolling="no"24 src="https://egghead.io/lessons/react-write-asynchronous-producers-in-immer-and-why-you-shouldn-t/embed"25 ></iframe>26 </div>27 <a28 className="egghead-link"29 href="https://egghead.io/lessons/react-write-asynchronous-producers-in-immer-and-why-you-shouldn-t"30 >31 Hosted on egghead.io32 </a>33</details>3435## `createDraft` and `finishDraft`3637`createDraft` 和 `finishDraft` 是两个底层函数,它们对于在 immer 之上构建抽象的库非常有用。它避免了为了使用 draft 始终创建函数。相反,人们可以创建一个 draft,对其进行修改,并在未来的某个时间完成该 draft,在这种情况下,将产生下一个不可变状态。例如,我们可以将上面的示例重写为:38

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 39why, one step back
39Beyond that, `createDraft` / `finishDraft` could be used to express async updates to drafts:

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

lines 40–56why, one step back
40
41```javascript42import {createDraft, finishDraft} from "immer"4344const user = {45 name: "michel",46 todos: []47}4849const draft = createDraft(user)50draft.todos = await (await window.fetch("http://host/" + draft.name)).json()51const loadedUser = finishDraft(draft)52```5354注意:`finishDraft` 以一个 `patchListener` 作为第二个参数,可以用来记录 patches,类似于 `produce`5556_警告:一般情况下,我们建议使用 `producer` 而不是 `createDraft / finishDraft` 组合,`produce` 在使用中不易出错,并且在代码中更清楚地区分了可变性和不变性的概念。_