immer

  • Version 10.1.1
  • Published
  • 627 kB
  • No dependencies
  • MIT license

Install

npm i immer
yarn add immer
pnpm add immer

Overview

Create your next immutable state by mutating the current one

Index

Variables

variable applyPatches

const applyPatches: any;
  • Apply an array of Immer patches to the first argument.

    This function is a producer, which means copy-on-write is in effect.

variable createDraft

const createDraft: any;
  • Create an Immer draft from the given base state, which may be a draft itself. The draft can be modified until you finalize it with the finishDraft function.

variable finishDraft

const finishDraft: any;
  • Finalize an Immer draft from a createDraft call, returning the base state (if no changes were made) or a modified copy. The draft must *not* be mutated afterwards.

    Pass a function as the 2nd argument to generate Immer patches based on the changes that were made.

variable immerable

const immerable: Symbol;
  • To let Immer treat your class instances as plain immutable objects (albeit with a custom prototype), you must define either an instance property or a static property on each of your custom classes.

    Otherwise, your class instance will never be drafted, which means it won't be safe to mutate in a produce callback.

variable nothing

const nothing: Symbol;
  • The sentinel value returned by producers to replace the draft with undefined.

variable produce

const produce: IProduce;
  • The produce function takes a value and a "recipe function" (whose return value often depends on the base state). The recipe function is free to mutate its first argument however it wants. All mutations are only ever applied to a __copy__ of the base state.

    Pass only a function to create a "curried producer" which relieves you from passing the recipe function every time.

    Only plain objects and arrays are made mutable. All other objects are considered uncopyable.

    Note: This function is __bound__ to its Immer instance.

    Parameter base

    the initial state

    Parameter producer

    function that receives a proxy of the base state as first argument and which can be freely modified

    Parameter patchListener

    optional function that will be called with all the patches produced here

    Returns

    {any} a new state, or the initial state if nothing was modified

variable produceWithPatches

const produceWithPatches: IProduceWithPatches;
  • Like produce, but produceWithPatches always returns a tuple [nextState, patches, inversePatches] (instead of just the next state)

variable setAutoFreeze

const setAutoFreeze: any;
  • Pass true to automatically freeze all copies created by Immer.

    Always freeze by default, even in production mode

variable setUseStrictShallowCopy

const setUseStrictShallowCopy: any;
  • Pass true to enable strict shallow copy.

    By default, immer does not copy the object descriptors such as getter, setter and non-enumrable properties.

Functions

function castDraft

castDraft: <T>(value: T) => Draft<T>;
  • This function is actually a no-op, but can be used to cast an immutable type to an draft type and make TypeScript happy

    Parameter value

function castImmutable

castImmutable: <T>(value: T) => Immutable<T>;
  • This function is actually a no-op, but can be used to cast a mutable type to an immutable type and make TypeScript happy

    Parameter value

function current

current: <T>(value: T) => T;
  • Takes a snapshot of the current state of a draft and finalizes it (but without freezing). This is a great utility to print the current state during debugging (no Proxies in the way). The output of current can also be safely leaked outside the producer.

function enableMapSet

enableMapSet: () => void;

    function enablePatches

    enablePatches: () => void;

      function freeze

      freeze: <T>(obj: T, deep?: boolean) => T;
      • Freezes draftable objects. Returns the original object. By default freezes shallowly, but if the second argument is true it will freeze recursively.

        Parameter obj

        Parameter deep

      function isDraft

      isDraft: (value: any) => boolean;
      • Returns true if the given value is an Immer draft

      function isDraftable

      isDraftable: (value: any) => boolean;
      • Returns true if the given value can be drafted by Immer

      function original

      original: <T>(value: T) => T;
      • Get the underlying object that is represented by the given draft

      Classes

      class Immer

      class Immer implements ProducersFns {}

        constructor

        constructor(config?: {
        autoFreeze?: boolean;
        useStrictShallowCopy?: StrictMode;
        });

          property autoFreeze_

          autoFreeze_: boolean;

            property produce

            produce: IProduce;
            • The produce function takes a value and a "recipe function" (whose return value often depends on the base state). The recipe function is free to mutate its first argument however it wants. All mutations are only ever applied to a __copy__ of the base state.

              Pass only a function to create a "curried producer" which relieves you from passing the recipe function every time.

              Only plain objects and arrays are made mutable. All other objects are considered uncopyable.

              Note: This function is __bound__ to its Immer instance.

              Parameter base

              the initial state

              Parameter recipe

              function that receives a proxy of the base state as first argument and which can be freely modified

              Parameter patchListener

              optional function that will be called with all the patches produced here

              Returns

              {any} a new state, or the initial state if nothing was modified

            property produceWithPatches

            produceWithPatches: IProduceWithPatches;

              property useStrictShallowCopy_

              useStrictShallowCopy_: StrictMode;

                method applyPatches

                applyPatches: <T extends Objectish>(base: T, patches: readonly Patch[]) => T;

                  method createDraft

                  createDraft: <T extends Objectish>(base: T) => Draft<T>;

                    method finishDraft

                    finishDraft: <D extends unknown>(
                    draft: D,
                    patchListener?: PatchListener
                    ) => D extends Draft<infer T> ? T : never;

                      method setAutoFreeze

                      setAutoFreeze: (value: boolean) => void;
                      • Pass true to automatically freeze all copies created by Immer.

                        By default, auto-freezing is enabled.

                      method setUseStrictShallowCopy

                      setUseStrictShallowCopy: (value: StrictMode) => void;
                      • Pass true to enable strict shallow copy.

                        By default, immer does not copy the object descriptors such as getter, setter and non-enumrable properties.

                      Interfaces

                      interface Patch

                      interface Patch {}

                        property op

                        op: 'replace' | 'remove' | 'add';

                          property path

                          path: (string | number)[];

                            property value

                            value?: any;

                              Type Aliases

                              type Draft

                              type Draft<T> = T extends PrimitiveType
                              ? T
                              : T extends AtomicObject
                              ? T
                              : T extends ReadonlyMap<infer K, infer V> // Map extends ReadonlyMap
                              ? Map<Draft<K>, Draft<V>>
                              : T extends ReadonlySet<infer V> // Set extends ReadonlySet
                              ? Set<Draft<V>>
                              : T extends WeakReferences
                              ? T
                              : T extends object
                              ? WritableDraft<T>
                              : T;
                              • Convert a readonly type into a mutable type, if possible

                              type Immutable

                              type Immutable<T> = T extends PrimitiveType
                              ? T
                              : T extends AtomicObject
                              ? T
                              : T extends ReadonlyMap<infer K, infer V> // Map extends ReadonlyMap
                              ? ReadonlyMap<Immutable<K>, Immutable<V>>
                              : T extends ReadonlySet<infer V> // Set extends ReadonlySet
                              ? ReadonlySet<Immutable<V>>
                              : T extends WeakReferences
                              ? T
                              : T extends object
                              ? { readonly [K in keyof T]: Immutable<T[K]> }
                              : T;
                              • Convert a mutable type into a readonly type

                              type Objectish

                              type Objectish = AnyObject | AnyArray | AnyMap | AnySet;

                                type PatchListener

                                type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void;

                                  type Producer

                                  type Producer<T> = (draft: Draft<T>) => ValidRecipeReturnType<Draft<T>>;
                                  • The type for recipe function

                                  type StrictMode

                                  type StrictMode = boolean | 'class_only';

                                    type WritableDraft

                                    type WritableDraft<T> = { -readonly [K in keyof T]: Draft<T[K]> };

                                      Package Files (9)

                                      Dependencies (0)

                                      No dependencies.

                                      Dev Dependencies (21)

                                      Peer Dependencies (0)

                                      No peer dependencies.

                                      Badge

                                      To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                      You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/immer.

                                      • Markdown
                                        [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/immer)
                                      • HTML
                                        <a href="https://www.jsdocs.io/package/immer"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>