@@ -7,22 +7,32 @@ export function defineContextualState<State>() {
77 React . SetStateAction < State >
88 > > ( null )
99
10- return [
11- ( props : {
12- children : React . ReactNode
13- initialState : State | ( ( ) => State )
14- } ) => {
15- const [ state , setState ] = React . useState ( props . initialState )
16- return (
17- < SetStateCtx . Provider value = { setState } >
18- < StateCtx . Provider value = { state } > { props . children } </ StateCtx . Provider >
19- </ SetStateCtx . Provider >
20- )
21- } ,
10+ function Provider ( props : {
11+ children : React . ReactNode
12+ initialState : State | ( ( ) => State )
13+ } ) {
14+ const [ state , setState ] = React . useState ( props . initialState )
15+ return (
16+ < SetStateCtx . Provider value = { setState } >
17+ < StateCtx . Provider value = { state } > { props . children } </ StateCtx . Provider >
18+ </ SetStateCtx . Provider >
19+ )
20+ }
2221
23- ( ) =>
24- [ React . useContext ( StateCtx ) , useInvariantContext ( SetStateCtx ) ] as const ,
22+ function useState ( ) {
23+ return [
24+ React . useContext ( StateCtx ) ,
25+ useInvariantContext ( SetStateCtx ) ,
26+ ] as const
27+ }
2528
26- ( ) => useInvariantContext ( SetStateCtx ) ,
27- ] as const
29+ function useSetState ( ) {
30+ return useInvariantContext ( SetStateCtx )
31+ }
32+
33+ return [ Provider , useState , useSetState ] as [
34+ Provider : typeof Provider ,
35+ useState : typeof useState ,
36+ useSetState : typeof useSetState ,
37+ ]
2838}
0 commit comments