@@ -47,7 +47,7 @@ export function useWindowSize() {
4747}
4848
4949export function useBoolean ( initialValue : boolean | null = null as any ) {
50- const [ state , setState ] = useState < boolean > ( initialValue )
50+ const [ state , setState ] = useState < boolean > ( initialValue as boolean )
5151
5252 const actions = {
5353 toggle ( ) {
@@ -63,20 +63,11 @@ export function useBoolean(initialValue: boolean | null = null as any) {
6363 setState ( v )
6464 } ,
6565 reset ( ) {
66- setState ( initialValue )
66+ setState ( initialValue as boolean )
6767 } ,
6868 }
6969
70- const end = [ state , actions ] as [
71- boolean ,
72- {
73- toggle ( ) : void
74- off ( ) : void
75- on ( ) : void
76- set ( v : boolean ) : void
77- reset ( ) : void
78- }
79- ]
70+ const end = [ state , actions ] as const
8071
8172 return end
8273}
@@ -85,6 +76,9 @@ export function useObject<T = any>(initialValue: T) {
8576 const [ state , setState ] = useState < T > ( initialValue )
8677
8778 const actions = {
79+ /**
80+ * @deprecated Use `setPartialValue` instead
81+ */
8882 write ( f : Partial < T > | ( ( e : T ) => Partial < T > ) ) {
8983 const n = (
9084 typeof f === "function" ? ( f as any ) ( state ) : { ...state , ...f }
@@ -95,31 +89,45 @@ export function useObject<T = any>(initialValue: T) {
9589 ...n ,
9690 } ) )
9791 } ,
92+ /**
93+ * @deprecated Use `setValue` instead
94+ */
9895 replace ( f : T | ( ( e : T ) => T ) ) {
9996 const n = typeof f === "function" ? ( f as any ) ( state ) : f
10097 setState ( n )
10198 } ,
10299
100+ setPartialValue ( f : Partial < T > | ( ( e : T ) => Partial < T > ) ) {
101+ const n = (
102+ typeof f === "function" ? ( f as any ) ( state ) : { ...state , ...f }
103+ ) as T
104+
105+ setState ( ( s ) => ( {
106+ ...s ,
107+ ...n ,
108+ } ) )
109+ } ,
110+ setValue ( f : T | ( ( e : T ) => T ) ) {
111+ const n = typeof f === "function" ? ( f as any ) ( state ) : f
112+ setState ( n )
113+ } ,
103114 reset ( ) {
104115 setState ( initialValue )
105116 } ,
106117 }
107118
108- const end = [ state , actions ] as [
109- T ,
110- {
111- write ( f : Partial < T > | ( ( e : T ) => Partial < T > ) ) : void
112- replace ( f : T | ( ( e : T ) => T ) ) : void
113- /**
114- * Reset to initial value
115- */
116- reset ( ) : void
117- }
118- ]
119+ const end = [ state , actions , setState ] as const
119120
120121 return end
121122}
122123
124+ function useE ( ) {
125+ const [ state , actions ] = useObject ( {
126+ name : "dany" ,
127+ email : "" ,
128+ } )
129+ }
130+
123131/**
124132 * Returns `true` after the component mounts/hydrates (after the first render)
125133 */
0 commit comments