Skip to content

Commit a02e129

Browse files
author
k.golikov
committed
Refactor produceState
1 parent 362816b commit a02e129

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

src/hooks/useStateProducer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Dispatch, SetStateAction, useCallback } from 'react';
22
import produceState, { StateProducerRecipe } from '../utils/produceState';
33

4-
const useStateProducer = <S extends object>(setState: Dispatch<SetStateAction<S>>) => {
4+
const useStateProducer = <S>(setState: Dispatch<SetStateAction<S>>) => {
55
return useCallback(
66
(recipe: StateProducerRecipe<S>) => {
77
produceState(setState, recipe);

src/utils/produceState.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
import { Dispatch, SetStateAction } from 'react';
22
import produce, { Draft } from 'immer';
3-
import { Nothing } from 'immer/src/internal';
43

5-
type ValidRecipeReturnType<State> = State | void | undefined | (State extends undefined ? Nothing : never);
4+
type RecipeReturnType<S> = S | void | undefined;
5+
export type StateProducerRecipe<S> = <D extends Draft<S>>(draft: D) => RecipeReturnType<D>;
66

7-
type RecipeReturnType<State> = ValidRecipeReturnType<State> | Promise<ValidRecipeReturnType<State>>;
8-
9-
export type StateProducerRecipe<S> = (draft: Draft<S>) => RecipeReturnType<Draft<S>>;
10-
11-
const produceState = <S extends object>(
7+
const produceState = <S>(
128
setState: Dispatch<SetStateAction<S>>,
139
recipe: StateProducerRecipe<S>
1410
): void | Promise<void> => {
1511
setState((state) => {
16-
return produce(state, recipe as any);
12+
return produce(state, recipe);
1713
});
1814
};
1915

0 commit comments

Comments
 (0)