use-synchronized-state 1.0.13 → 1.0.15
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/CHANGELOG.md +45 -0
- package/README.md +49 -8
- package/package.json +6 -5
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
## 1.0.15 (Jan 8, 2026)
|
|
2
|
+
- Correct links in package.json
|
|
3
|
+
|
|
4
|
+
## 1.0.14 (Jan 8, 2026)
|
|
5
|
+
- Add CHANGELOG.md to the list of exported files
|
|
6
|
+
|
|
7
|
+
## 1.0.13 (Jan 8, 2026)
|
|
8
|
+
- Correct docs
|
|
9
|
+
- Add CHANGELOG.md
|
|
10
|
+
|
|
11
|
+
## 1.0.12 (Jan 8, 2026)
|
|
12
|
+
- Cleanup the "files" field in package.json
|
|
13
|
+
|
|
14
|
+
## 1.0.11 (Jan 8, 2026)
|
|
15
|
+
- Mimic the useState hook's behavior when setting the same state (add bailing out)
|
|
16
|
+
|
|
17
|
+
## 1.0.10 (Jan 8, 2026)
|
|
18
|
+
- Prevent re-renders when setting the same state
|
|
19
|
+
|
|
20
|
+
## 1.0.8 (Jan 8, 2026)
|
|
21
|
+
- Add an extra keyword
|
|
22
|
+
|
|
23
|
+
## 1.0.7 (Jan 8, 2026)
|
|
24
|
+
- Improve the packaging
|
|
25
|
+
|
|
26
|
+
## 1.0.6 (Jan 8, 2026)
|
|
27
|
+
- Cleanup docs
|
|
28
|
+
|
|
29
|
+
## 1.0.5 (Jan 8, 2026)
|
|
30
|
+
- Fix a bug with the rendering
|
|
31
|
+
|
|
32
|
+
## 1.0.4 (Jan 7, 2026)
|
|
33
|
+
- Fix docs
|
|
34
|
+
|
|
35
|
+
## 1.0.3 (Jan 7, 2026)
|
|
36
|
+
- Fix docs
|
|
37
|
+
|
|
38
|
+
## 1.0.2 (Jan 7, 2026)
|
|
39
|
+
- Add keywords to package.json
|
|
40
|
+
|
|
41
|
+
## 1.0.1 (Jan 7, 2026)
|
|
42
|
+
- Improve docs
|
|
43
|
+
|
|
44
|
+
## 1.0.0 (Jan 7, 2026)
|
|
45
|
+
- First version
|
package/README.md
CHANGED
|
@@ -21,22 +21,63 @@ or
|
|
|
21
21
|
|
|
22
22
|
## Usage
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
### Synchronize state with prop
|
|
25
|
+
|
|
26
|
+
```typescript jsx
|
|
25
27
|
import { useState } from 'react';
|
|
26
|
-
import { useSyncState } from 'use-synchronized-state';
|
|
27
28
|
|
|
28
29
|
function ParentComponent() {
|
|
29
30
|
const [parentState, setParentState] = useState(0);
|
|
30
31
|
|
|
31
32
|
return (
|
|
32
|
-
<
|
|
33
|
+
<div>
|
|
34
|
+
<button onClick={() => {setParentState(prev => prev + 1);}}>
|
|
35
|
+
increment parentState
|
|
36
|
+
</button>
|
|
37
|
+
<div>parentState = {parentState}</div>
|
|
38
|
+
<ChildComponent parentState={parentState} />
|
|
39
|
+
</div>
|
|
33
40
|
);
|
|
34
41
|
}
|
|
35
42
|
|
|
43
|
+
import { useSyncState } from 'use-synchronized-state';
|
|
44
|
+
|
|
36
45
|
function ChildComponent({ parentState }: { parentState: number }) {
|
|
37
46
|
// this syncState is synchronized with parentState (has the same api as useState)
|
|
38
47
|
const [syncState, setSyncState] = useSyncState(parentState);
|
|
39
|
-
|
|
48
|
+
|
|
49
|
+
return (
|
|
50
|
+
<div>
|
|
51
|
+
<button onClick={() => {setSyncState(prev => prev + 1);}}>
|
|
52
|
+
increment syncState
|
|
53
|
+
</button>
|
|
54
|
+
<div>syncState = {syncState}</div>
|
|
55
|
+
</div>
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Synchronize state with memoized value
|
|
61
|
+
|
|
62
|
+
```typescript jsx
|
|
63
|
+
import { useMemo } from 'react';
|
|
64
|
+
import { useSyncState } from 'use-synchronized-state';
|
|
65
|
+
|
|
66
|
+
function ChildComponent({ parentState }: { parentState: number }) {
|
|
67
|
+
// compute a value based on parentState
|
|
68
|
+
const complexState = useMemo(() => parentState + 1, [parentState]);
|
|
69
|
+
|
|
70
|
+
// this syncState is synchronized with complexState (has the same api as useState)
|
|
71
|
+
const [syncState, setSyncState] = useSyncState(complexState);
|
|
72
|
+
|
|
73
|
+
return (
|
|
74
|
+
<div>
|
|
75
|
+
<button onClick={() => {setSyncState(prev => prev + 1);}}>
|
|
76
|
+
increment syncState
|
|
77
|
+
</button>
|
|
78
|
+
<div>syncState = {syncState}</div>
|
|
79
|
+
</div>
|
|
80
|
+
);
|
|
40
81
|
}
|
|
41
82
|
```
|
|
42
83
|
|
|
@@ -49,7 +90,7 @@ the returned state changes to the same value, but it can also change independent
|
|
|
49
90
|
|
|
50
91
|
### Let's see a small example
|
|
51
92
|
|
|
52
|
-
```typescript
|
|
93
|
+
```typescript jsx
|
|
53
94
|
import { useState } from 'react';
|
|
54
95
|
|
|
55
96
|
function ParentComponent() {
|
|
@@ -79,7 +120,7 @@ What can we do in the following situation?
|
|
|
79
120
|
|
|
80
121
|
The first thing that comes to mind is to do something like this:
|
|
81
122
|
|
|
82
|
-
```typescript
|
|
123
|
+
```typescript jsx
|
|
83
124
|
function ChildComponent({ parentState }: { parentState: number }) {
|
|
84
125
|
const [syncState, setSyncState] = useState(parentState);
|
|
85
126
|
|
|
@@ -112,7 +153,7 @@ I have changed the above example to match it. It still rerenders the ChildCompon
|
|
|
112
153
|
performance penalty, is hard to reason about and works only for states defined in the same component
|
|
113
154
|
(if syncState was a prop, it wouldn't work).
|
|
114
155
|
|
|
115
|
-
```typescript
|
|
156
|
+
```typescript jsx
|
|
116
157
|
function ChildComponent({ parentState }: { parentState: number }) {
|
|
117
158
|
const [syncState, setSyncState] = useState(parentState);
|
|
118
159
|
|
|
@@ -131,7 +172,7 @@ if the reactive value changes (for example setParentState(1) is called), both pa
|
|
|
131
172
|
the same value from the first render (after the setParentState) and there will be no additional render (only the needed one).
|
|
132
173
|
|
|
133
174
|
|
|
134
|
-
```typescript
|
|
175
|
+
```typescript jsx
|
|
135
176
|
import { useSyncState } from 'use-synchronized-state';
|
|
136
177
|
|
|
137
178
|
function ChildComponent({ parentState }: { parentState: number }) {
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "use-synchronized-state",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.15",
|
|
4
4
|
"description": "A React hook that creates a synchronized state with a reactive value in react (fixing the Cascading updates issue)",
|
|
5
|
-
"homepage": "https://github.com/rhorge/use-
|
|
5
|
+
"homepage": "https://github.com/rhorge/use-synchronized-state#readme",
|
|
6
6
|
"bugs": {
|
|
7
|
-
"url": "https://github.com/rhorge/use-
|
|
7
|
+
"url": "https://github.com/rhorge/use-synchronized-state/issues"
|
|
8
8
|
},
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
11
|
-
"url": "git+https://github.com/rhorge/use-
|
|
11
|
+
"url": "git+https://github.com/rhorge/use-synchronized-state.git"
|
|
12
12
|
},
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"author": "rhorge",
|
|
@@ -67,7 +67,8 @@
|
|
|
67
67
|
"typescript": "^3.8.3"
|
|
68
68
|
},
|
|
69
69
|
"files": [
|
|
70
|
-
"/dist/*"
|
|
70
|
+
"/dist/*",
|
|
71
|
+
"CHANGELOG.md"
|
|
71
72
|
],
|
|
72
73
|
"keywords": [
|
|
73
74
|
"react",
|