synstate-react-hooks 0.1.1 → 0.1.2
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 +114 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# SynState React hooks
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm add synstate-react-hooks
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Or with other package managers:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
# Yarn
|
|
13
|
+
yarn add synstate-react-hooks
|
|
14
|
+
|
|
15
|
+
# pnpm
|
|
16
|
+
pnpm add synstate-react-hooks
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Quick Start
|
|
20
|
+
|
|
21
|
+
### Simple State Management
|
|
22
|
+
|
|
23
|
+
```tsx
|
|
24
|
+
import type * as React from 'react';
|
|
25
|
+
import { createState } from 'synstate-react-hooks';
|
|
26
|
+
|
|
27
|
+
// Global state (outside component)
|
|
28
|
+
const [useUserState, setUserState] = createState({
|
|
29
|
+
name: '',
|
|
30
|
+
email: '',
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const UserProfile = (): React.JSX.Element => {
|
|
34
|
+
const user = useUserState();
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
<div>
|
|
38
|
+
<p>
|
|
39
|
+
{'Name: '}
|
|
40
|
+
{user.name}
|
|
41
|
+
</p>
|
|
42
|
+
<button
|
|
43
|
+
onClick={() => {
|
|
44
|
+
setUserState({
|
|
45
|
+
name: 'Alice',
|
|
46
|
+
email: 'alice@example.com',
|
|
47
|
+
});
|
|
48
|
+
}}
|
|
49
|
+
>
|
|
50
|
+
{'Set User'}
|
|
51
|
+
</button>
|
|
52
|
+
</div>
|
|
53
|
+
);
|
|
54
|
+
};
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
With `resetState`, `updateState`, `state` APIs:
|
|
58
|
+
|
|
59
|
+
```tsx
|
|
60
|
+
import type * as React from 'react';
|
|
61
|
+
import { createState } from 'synstate-react-hooks';
|
|
62
|
+
|
|
63
|
+
// Global state (outside component)
|
|
64
|
+
const [
|
|
65
|
+
useUserState,
|
|
66
|
+
setUserState,
|
|
67
|
+
{
|
|
68
|
+
resetState: resetUserState,
|
|
69
|
+
updateState: updateUserState,
|
|
70
|
+
state: userState,
|
|
71
|
+
},
|
|
72
|
+
] = createState({
|
|
73
|
+
name: '',
|
|
74
|
+
email: '',
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
userState.subscribe((u) => {
|
|
78
|
+
console.log('User is updated:', u);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const UserProfile = (): React.JSX.Element => {
|
|
82
|
+
const user = useUserState();
|
|
83
|
+
|
|
84
|
+
return (
|
|
85
|
+
<div>
|
|
86
|
+
<p>
|
|
87
|
+
{'Name: '}
|
|
88
|
+
{user.name}
|
|
89
|
+
</p>
|
|
90
|
+
<button
|
|
91
|
+
onClick={() => {
|
|
92
|
+
setUserState({
|
|
93
|
+
name: 'Alice',
|
|
94
|
+
email: 'alice@example.com',
|
|
95
|
+
});
|
|
96
|
+
}}
|
|
97
|
+
>
|
|
98
|
+
{'Set User'}
|
|
99
|
+
</button>
|
|
100
|
+
<button
|
|
101
|
+
onClick={() => {
|
|
102
|
+
updateUserState((prev) => ({
|
|
103
|
+
name: prev.name,
|
|
104
|
+
email: '',
|
|
105
|
+
}));
|
|
106
|
+
}}
|
|
107
|
+
>
|
|
108
|
+
{'Reset email'}
|
|
109
|
+
</button>
|
|
110
|
+
<button onClick={resetUserState}>{'Reset'}</button>
|
|
111
|
+
</div>
|
|
112
|
+
);
|
|
113
|
+
};
|
|
114
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "synstate-react-hooks",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Type-safe State Management Library for TypeScript/JavaScript",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
],
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"ts-data-forge": "^6.5.0",
|
|
31
|
-
"synstate": "0.1.
|
|
31
|
+
"synstate": "0.1.2"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@types/argparse": "^2.0.17",
|