@@ -6,19 +6,21 @@ import type { RGS, SetStateAction, ValueType } from "./utils";
6
6
export type { SetterArgType , SetStateAction , Plugin } from "./utils" ;
7
7
8
8
/**
9
- * Use this hook similar to `useState` hook.
10
- * The difference is that you need to pass a
11
- * unique key - unique across the app to make
12
- * this state accessible to all client components.
9
+ * A React hook for managing shared global state, similar to the `useState` hook.
10
+ * This hook requires a unique key, which identifies the global store and allows state sharing across all client components.
13
11
*
14
12
* @example
15
13
* ```tsx
16
14
* const [state, setState] = useRGS<number>("counter", 1);
17
15
* ```
18
16
*
19
- * @param key - Unique key to identify the store.
20
- * @param value - Initial value of the store.
21
- * @returns - A tuple (Ordered sequance of values) containing the state and a function to set the state.
17
+ * @param key - A unique key to identify the global store.
18
+ * @param value - The initial value of the global state. Can be a value or a function returning a value.
19
+ * @param includeRegExp - (Optional) A regular expression to specify which fields trigger updates.
20
+ * @param excludeRegExp - (Optional) A regular expression to specify which fields should be excluded from updates.
21
+ * @returns A tuple containing the current state and a function to update the state.
22
+ *
23
+ * @see [Learn More](https://r18gs.vercel.app/)
22
24
*/
23
25
const useRGS = < T > (
24
26
key : string ,
@@ -27,7 +29,7 @@ const useRGS = <T>(
27
29
excludeRegExp ?: RegExp ,
28
30
) : [ T , SetStateAction < T > ] => {
29
31
/** Initialize the named store when invoked for the first time. */
30
- if ( ! globalRGS [ key ] )
32
+ if ( ! globalRGS [ key ] ) {
31
33
globalRGS [ key ] = {
32
34
v : value instanceof Function ? value ( ) : value ,
33
35
l : [ ] ,
@@ -38,6 +40,7 @@ const useRGS = <T>(
38
40
triggerListeners ( rgs , oldV , rgs . v ) ;
39
41
} ,
40
42
} ;
43
+ }
41
44
42
45
return createHook < T > ( key , includeRegExp , excludeRegExp ) ;
43
46
} ;
0 commit comments