File tree Expand file tree Collapse file tree 3 files changed +26
-24
lines changed Expand file tree Collapse file tree 3 files changed +26
-24
lines changed Original file line number Diff line number Diff line change 11import { Dispatch , SetStateAction , useCallback } from 'react' ;
2- import produce , { Draft } from 'immer' ;
3- import { Nothing } from 'immer/src/internal' ;
4-
5- type ValidRecipeReturnType < State > = State | void | undefined | ( State extends undefined ? Nothing : never ) ;
6-
7- type RecipeReturnType < State > = ValidRecipeReturnType < State > | Promise < ValidRecipeReturnType < State > > ;
8-
9- type Recipe < S > = ( draft : Draft < S > ) => RecipeReturnType < Draft < S > > ;
10-
11- export const produceState = < S extends object > (
12- setState : Dispatch < SetStateAction < S > > ,
13- recipe : Recipe < S >
14- ) : void | Promise < void > => {
15- setState ( ( state ) => {
16- return produce ( state , recipe as any ) ;
17- } ) ;
18- } ;
2+ import produceState , { StateProducerRecipe } from '../utils/produceState' ;
193
204const useStateProducer = < S extends object > ( setState : Dispatch < SetStateAction < S > > ) => {
215 return useCallback (
22- ( recipe : Recipe < S > ) => {
6+ ( recipe : StateProducerRecipe < S > ) => {
237 produceState ( setState , recipe ) ;
248 } ,
259 [ setState ]
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import styles from './DataUrlViewPage.module.scss';
44import { Navigate } from 'react-router-dom' ;
55import { routes } from '../../constants/router/routes' ;
66import useRouteContextEffect from '../../hooks/useRouteContextEffect' ;
7+ import produceState from '../../utils/produceState' ;
78
89export interface DataUrlViewPageQueryParams {
910 data ?: string ;
@@ -19,12 +20,9 @@ const DataUrlViewPage: FunctionComponent = () => {
1920
2021 useRouteContextEffect (
2122 ( setRouteContentState ) => {
22- const newTitle = title ?? routes . dataUrlView . title ;
23-
24- setRouteContentState ( ( context ) => ( {
25- ...context ,
26- title : newTitle
27- } ) ) ;
23+ produceState ( setRouteContentState , ( context ) => {
24+ context . title = title ?? routes . dataUrlView . title ;
25+ } ) ;
2826 } ,
2927 [ title ]
3028 ) ;
Original file line number Diff line number Diff line change 1+ import { Dispatch , SetStateAction } from 'react' ;
2+ import produce , { Draft } from 'immer' ;
3+ import { Nothing } from 'immer/src/internal' ;
4+
5+ type ValidRecipeReturnType < State > = State | void | undefined | ( State extends undefined ? Nothing : never ) ;
6+
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 > (
12+ setState : Dispatch < SetStateAction < S > > ,
13+ recipe : StateProducerRecipe < S >
14+ ) : void | Promise < void > => {
15+ setState ( ( state ) => {
16+ return produce ( state , recipe as any ) ;
17+ } ) ;
18+ } ;
19+
20+ export default produceState ;
You can’t perform that action at this time.
0 commit comments