Visitar URL original
use-mutative/docs/modules.md at main · mutativejs/use-mutative · GitHub
Skip to content

Latest commit

 

History

History
388 lines (210 loc) · 6.59 KB

modules.md

File metadata and controls

388 lines (210 loc) · 6.59 KB

use-mutative


use-mutative

Type Aliases

DraftFunction

DraftFunction<S> = (draft) => void

Defined in: index.ts:20

Type Parameters

S

S

Parameters

draft

Draft<S>

Returns

void


InitialValue

InitialValue<I> = I extends (...args) => infer R ? R : I

Defined in: index.ts:23

Type Parameters

I

I extends any


Reducer

Reducer<S, A> = (draftState, action) => void | S | undefined

Defined in: index.ts:149

Type Parameters

S

S

A

A

Parameters

draftState

Draft<S>

action

A

Returns

void | S | undefined


ReducerResult

ReducerResult<S, A, O, F> = O extends true | object ? [F extends true ? Immutable<S> : S, Dispatch<A>, Patches<O>, Patches<O>] : F extends true ? [Immutable<S>, Dispatch<A>] : [S, Dispatch<A>]

Defined in: index.ts:138

Type Parameters

S

S

A

A

O

O extends PatchesOptions

F

F extends boolean


Result

Result<S, O, F> = O extends true | object ? [F extends true ? Immutable<S> : S, Updater<S>, Patches<O>, Patches<O>] : F extends true ? [Immutable<S>, Updater<S>] : [S, Updater<S>]

Defined in: index.ts:25

Type Parameters

S

S

O

O extends PatchesOptions

F

F extends boolean


Updater

Updater<S> = (value) => void

Defined in: index.ts:21

Type Parameters

S

S

Parameters

value

S | (() => S) | DraftFunction<S>

Returns

void

Functions

useMutative()

useMutative<S, F, O>(initialValue, options?): Result<InitialValue<S>, O, F>

Defined in: index.ts:54

useMutative is a hook that is similar to useState but it uses mutative to handle the state updates.

Type Parameters

S

S

F

F extends boolean = false

O

O extends PatchesOptions = false

Parameters

initialValue

S

The initial state. You may optionally provide an initializer function to calculate the initial state.

options?

ExternalOptions<O, F>

Options for the useMutative hook.

Returns

Result<InitialValue<S>, O, F>

Example

import { act, renderHook } from '@testing-library/react';

import { useMutative } from '../src/index';

const { result } = renderHook(() => useMutative({ items: [1] }));
const [state, updateState] = result.current;
act(() =>
  updateState((draft) => {
    draft.items.push(2);
  })
);
const [nextState] = result.current;
expect(nextState).toEqual({ items: [1, 2] });

useMutativeReducer()

useMutativeReducer is a hook that is similar to useReducer but it uses mutative to handle the state updates.

Example

import { act, renderHook } from '@testing-library/react';
import { type Draft } from 'mutative';

import { useMutativeReducer } from '../src/index';

const { result } = renderHook(() =>
  useMutativeReducer(
    (
      draft: Draft<Readonly<{ count: number }>>,
      action: {
        type: 'increment';
      }
    ) => {
      switch (action.type) {
        case 'increment':
          draft.count += 1;
      }
    },
    { count: 0 }
  )
);
const [, dispatch] = result.current;
act(() => dispatch({ type: 'increment' }));
expect(result.current[0]).toEqual({ count: 1 });

Call Signature

useMutativeReducer<S, A, I, F, O>(reducer, initializerArg, initializer, options?): ReducerResult<S, A, O, F>

Defined in: index.ts:151

Type Parameters
S

S

A

A

I

I

F

F extends boolean = false

O

O extends PatchesOptions = false

Parameters
reducer

Reducer<S, A>

initializerArg

S & I

initializer

(arg) => S

options?

ExternalOptions<O, F>

Returns

ReducerResult<S, A, O, F>

Call Signature

useMutativeReducer<S, A, I, F, O>(reducer, initializerArg, initializer, options?): ReducerResult<S, A, O, F>

Defined in: index.ts:164

Type Parameters
S

S

A

A

I

I

F

F extends boolean = false

O

O extends PatchesOptions = false

Parameters
reducer

Reducer<S, A>

initializerArg

I

initializer

(arg) => S

options?

ExternalOptions<O, F>

Returns

ReducerResult<S, A, O, F>

Call Signature

useMutativeReducer<S, A, F, O>(reducer, initialState, initializer?, options?): ReducerResult<S, A, O, F>

Defined in: index.ts:177

Type Parameters
S

S

A

A

F

F extends boolean = false

O

O extends PatchesOptions = false

Parameters
reducer

Reducer<S, A>

initialState

S

initializer?

undefined

options?

ExternalOptions<O, F>

Returns

ReducerResult<S, A, O, F>