use-mutative / Exports
Ƭ DraftFunction<S
>: (draft
: Draft
<S
>) => void
Name |
---|
S |
▸ (draft
): void
Name | Type |
---|---|
draft |
Draft <S > |
void
Ƭ Reducer<S
, A
>: (draftState
: Draft
<S
>, action
: A
) => void
| S
| undefined
Name |
---|
S |
A |
▸ (draftState
, action
): void
| S
| undefined
Name | Type |
---|---|
draftState |
Draft <S > |
action |
A |
void
| S
| undefined
Ƭ Updater<S
>: (value
: S
| () => S
| DraftFunction
<S
>) => void
Name |
---|
S |
▸ (value
): void
Name | Type |
---|---|
value |
S | () => S | DraftFunction <S > |
void
▸ useMutative<S
, F
, O
>(initialValue
, options?
): Result
<InitialValue
<S
>, O
, F
>
useMutative
is a hook that is similar to useState
but it uses mutative
to handle the state updates.
Name | Type |
---|---|
S |
S |
F |
extends boolean = false |
O |
extends PatchesOptions = false |
Name | Type | Description |
---|---|---|
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. |
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, setState] = result.current;
act(() =>
setState((draft) => {
draft.items.push(2);
})
);
const [nextState] = result.current;
expect(nextState).toEqual({ items: [1, 2] });
▸ useMutativeReducer<S
, A
, I
, F
, O
>(reducer
, initializerArg
, initializer
, options?
): ReducerResult
<S
, A
, O
, F
>
useMutativeReducer
is a hook that is similar to useReducer
but it uses mutative
to handle the state updates.
Name | Type |
---|---|
S |
S |
A |
A |
I |
I |
F |
extends boolean = false |
O |
extends PatchesOptions = false |
Name | Type |
---|---|
reducer |
Reducer <S , A > |
initializerArg |
S & I |
initializer |
(arg : S & I ) => S |
options? |
ExternalOptions <O , F > |
ReducerResult
<S
, A
, O
, F
>
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 });
▸ useMutativeReducer<S
, A
, I
, F
, O
>(reducer
, initializerArg
, initializer
, options?
): ReducerResult
<S
, A
, O
, F
>
useMutativeReducer
is a hook that is similar to useReducer
but it uses mutative
to handle the state updates.
Name | Type |
---|---|
S |
S |
A |
A |
I |
I |
F |
extends boolean = false |
O |
extends PatchesOptions = false |
Name | Type |
---|---|
reducer |
Reducer <S , A > |
initializerArg |
I |
initializer |
(arg : I ) => S |
options? |
ExternalOptions <O , F > |
ReducerResult
<S
, A
, O
, F
>
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 });
▸ useMutativeReducer<S
, A
, F
, O
>(reducer
, initialState
, initializer?
, options?
): ReducerResult
<S
, A
, O
, F
>
useMutativeReducer
is a hook that is similar to useReducer
but it uses mutative
to handle the state updates.
Name | Type |
---|---|
S |
S |
A |
A |
F |
extends boolean = false |
O |
extends PatchesOptions = false |
Name | Type |
---|---|
reducer |
Reducer <S , A > |
initialState |
S |
initializer? |
undefined |
options? |
ExternalOptions <O , F > |
ReducerResult
<S
, A
, O
, F
>
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 });