Skip to content
Myr

api.md

This file

Path

website/docs/api.md

43 lines · 3.8 KiB · Markdown

Last changed by

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

History of this file

Take it

docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95

lines 1–6why, one step back
1---
2id: api3title: API overview4---56<center>

chore: docusaurus upgradeMichel Weststratebefore adoption48a91b3

line 7why, one step back
7<div data-ea-publisher="immerjs" data-ea-type="image" className="horizontal bordered"></div>

docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95

lines 8–11why, one step back
8</center>
910| Exported name | Description | Section |11| --- | --- | --- |

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

line 12why, one step back
12| `produce` | The core API of Immer: `import {produce} from "immer"` | [Produce](./produce.mdx) |

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

lines 13–21why, one step back
13| `applyPatches` | Given a base state or draft, and a set of patches, applies the patches | [Patches](./patches.mdx) |
14| `castDraft` | Converts any immutable type to its mutable counterpart. This is just a cast and doesn't actually do anything. | [TypeScript](./typescript.mdx) |15| `castImmutable` | Converts any mutable type to its immutable counterpart. This is just a cast and doesn't actually do anything. | [TypeScript](./typescript.mdx) |16| `createDraft` | Given a base state, creates a mutable draft for which any modifications will be recorded | [Async](./async.mdx) |17| `current` | Given a draft object (doesn't have to be a tree root), takes a snapshot of the current state of the draft | [Current](./current.md) |18| `Draft<T>` | Exposed TypeScript type to convert an immutable type to a mutable type | [TypeScript](./typescript.mdx) |19| `enableMapSet()` | Enables support for `Map` and `Set` collections. | [Installation](./installation.mdx#pick-your-immer-version) |20| `enablePatches()` | Enables support for JSON patches. | [Installation](./installation#pick-your-immer-version) |21| `finishDraft` | Given an draft created using `createDraft`, seals the draft and produces and returns the next immutable state that captures all the changes | [Async](./async.mdx) |

docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95

lines 22–23why, one step back
22| `freeze(obj, deep?)` | Freezes draftable objects. Returns the original object. By default freezes shallowly, but if the second argument is `true` it will freeze recursively. |
23| `Immer` | constructor that can be used to create a second "immer" instance (exposing all APIs listed in this instance), that doesn't share its settings with global instance. |

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

line 24why, one step back
24| `immerable` | Symbol that can be added to a constructor or prototype, to indicate that Immer should treat the class as something that can be safely drafted | [Classes](./complex-objects.md) |

docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95

lines 25–27why, one step back
25| `Immutable<T>` | Exposed TypeScript type to convert mutable types to immutable types | |
26| `isDraft` | Returns true if the given object is a draft object | |27| `isDraftable` | Returns true if Immer is capable of turning this object into a draft. Which is true for: arrays, objects without prototype, objects with `Object` as their prototype, objects that have the `immerable` symbol on their constructor or prototype | |

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

lines 28–32why, one step back
28| `nothing` | Value that can be returned from a recipe, to indicate that the value `undefined` should be produced | [Return](./return.mdx) |
29| `original` | Given a draft object (doesn't have to be a tree root), returns the original object at the same path in the original state tree, if present | [Original](./original.md) |30| `Patch` | Exposed TypeScript type, describes the shape of an (inverse) patch object | [Patches](./patches.mdx) |31| `produceWithPatches` | Works the same as `produce`, but instead of just returning the produced object, it returns a tuple, consisting of `[result, patches, inversePatches]`. | [Patches](./patches.mdx) |32| `setAutoFreeze` | Enables / disables automatic freezing of the trees produces. By default enabled. | [Freezing](./freezing.mdx) |

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

line 33why, one step back
33| `setUseStrictShallowCopy` | Can be used to enable strict shallow copy. If enable, immer copies non-enumerable properties as much as possible. | [Classes](./complex-objects.md) |

docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95

lines 34–36why, one step back
34
35## Importing immer36

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

line 37why, one step back
37In most cases, the only thing you need to import from Immer is `produce`:

docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95

lines 38–41why, one step back
38
39```javascript40import {produce} from "immer"41```

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

lines 42–43why, one step back
42
43Note that in older versions, `produce` was also available as default export (e.g. `import produce from "immer"` was also valid, but that is no longer the case to improve eco system compatibility.