Skip to content
Myr

original.md

This file

Path

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

34 lines · 1.0 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: original3title: 从 draft 中提取原始 state4sidebar_label: Original5---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中暴露了一个命名对象 `original`,将从 `produce` 内部的代理实例获取原始对象(对于未代理值返回 `undefined`。一个好的例子是:当在一个树状 state 中使用严格相等搜索结点的时候它很有用。

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

lines 12–34why, one step back
12
13```js14import {original, produce} from "immer"1516const baseState = {users: [{name: "Richie"}]}17const nextState = produce(baseState, draftState => {18 original(draftState.users) // is === baseState.users19})20```2122只是想知道一个值是否是代理实例?使用 `isDraft` 函数!请注意,不能在不是 draft 的对象上调用 `original`。2324```js25import {isDraft, produce} from "immer"2627const baseState = {users: [{name: "Bobby"}]}28const nextState = produce(baseState, draft => {29 isDraft(draft) // => true30 isDraft(draft.users) // => true31 isDraft(draft.users[0]) // => true32})33isDraft(nextState) // => false34```