react-use-singleton-hook 1.0.15 → 1.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -24,6 +24,56 @@ pnpm add react-use-singleton-hook
|
|
|
24
24
|
|
|
25
25
|
This assumes that you’re using [npm](http://npmjs.com/) package manager
|
|
26
26
|
|
|
27
|
+
## What is a singleton hook
|
|
28
|
+
|
|
29
|
+
- **Works like React Context**: A singleton hook encapsulates shared state logic and exposes it to any component that calls the hook, similar to how context provides values to consumers.
|
|
30
|
+
- **Mounts lazily**: The hook logic runs only when the first component uses it. After mounting, it stays active for the app’s lifetime unless configured to unmount.
|
|
31
|
+
- **No setup required**: No need for context providers or changes to your app structure. The hook uses React’s built-in hooks and a hidden DOM node for efficient, portable state management.
|
|
32
|
+
|
|
33
|
+
## Examples
|
|
34
|
+
|
|
35
|
+
#### dark/light mode switch
|
|
36
|
+
Whenever `Configurator` changes darkMode, all subscribed components are updated.
|
|
37
|
+
|
|
38
|
+
```javascript
|
|
39
|
+
/*************** file:src/services/darkMode.js ***************/
|
|
40
|
+
import { useState } from 'react';
|
|
41
|
+
import { createSingletonGlobalState } from 'react-use-singleton-hook';
|
|
42
|
+
|
|
43
|
+
const initDarkMode = false;
|
|
44
|
+
let globalSetMode = () => { throw new Error('you must useDarkMode before setting its state'); };
|
|
45
|
+
|
|
46
|
+
export const useDarkMode = createSingletonGlobalState(initDarkMode, () => {
|
|
47
|
+
const [mode, setMode] = useState(initDarkMode);
|
|
48
|
+
globalSetMode = setMode;
|
|
49
|
+
return mode;
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
export const setDarkMode = mode => globalSetMode(mode);
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
/*************** file:src/compoents/App.js ***************/
|
|
56
|
+
|
|
57
|
+
import React from 'react';
|
|
58
|
+
import { useDarkMode, setDarkMode } from 'src/services/darkMode';
|
|
59
|
+
|
|
60
|
+
const Consumer1 = () => {
|
|
61
|
+
const mode = useDarkMode();
|
|
62
|
+
return <div className={`is-dark-${mode}`}>Consumer1 - {`${mode}`}</div>;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const Consumer2 = () => {
|
|
66
|
+
const mode = useDarkMode();
|
|
67
|
+
return <div className={`is-dark-${mode}`}>Consumer2 - {`${mode}`}</div>;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const Configurator = () => {
|
|
71
|
+
const mode = useDarkMode();
|
|
72
|
+
return <button onClick={() => setDarkMode(!mode)}>Toggle dark/light</button>;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
```
|
|
76
|
+
|
|
27
77
|
### Compatibility
|
|
28
78
|
|
|
29
79
|
- ✅ Supports React 18.x and 19.x
|