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 1
1
import { 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' ;
19
3
20
4
const useStateProducer = < S extends object > ( setState : Dispatch < SetStateAction < S > > ) => {
21
5
return useCallback (
22
- ( recipe : Recipe < S > ) => {
6
+ ( recipe : StateProducerRecipe < S > ) => {
23
7
produceState ( setState , recipe ) ;
24
8
} ,
25
9
[ setState ]
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import styles from './DataUrlViewPage.module.scss';
4
4
import { Navigate } from 'react-router-dom' ;
5
5
import { routes } from '../../constants/router/routes' ;
6
6
import useRouteContextEffect from '../../hooks/useRouteContextEffect' ;
7
+ import produceState from '../../utils/produceState' ;
7
8
8
9
export interface DataUrlViewPageQueryParams {
9
10
data ?: string ;
@@ -19,12 +20,9 @@ const DataUrlViewPage: FunctionComponent = () => {
19
20
20
21
useRouteContextEffect (
21
22
( 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
+ } ) ;
28
26
} ,
29
27
[ title ]
30
28
) ;
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