use-synchronized-state 1.0.14 → 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/CHANGELOG.md +9 -0
- package/README.md +56 -13
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
## 1.0.16 (Jan 23, 2026)
|
|
2
|
+
- Make the hook compatible with the Fiber algorithm
|
|
3
|
+
|
|
4
|
+
## 1.0.15 (Jan 8, 2026)
|
|
5
|
+
- Correct links in package.json
|
|
6
|
+
|
|
7
|
+
## 1.0.14 (Jan 8, 2026)
|
|
8
|
+
- Add CHANGELOG.md to the list of exported files
|
|
9
|
+
|
|
1
10
|
## 1.0.13 (Jan 8, 2026)
|
|
2
11
|
- Correct docs
|
|
3
12
|
- Add CHANGELOG.md
|
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# A React hook that creates a synchronized state with a reactive value
|
|
1
|
+
# A React hook that creates a synchronized state with a reactive value (fixing the Cascading updates issue)
|
|
2
2
|
|
|
3
3
|
## Highlights
|
|
4
4
|
- Offers a hook called useSyncState
|
|
@@ -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
|
|
|
@@ -108,11 +149,12 @@ This is the cascading updates problem in all its glory, instead of having one re
|
|
|
108
149
|
|
|
109
150
|
What react docs tell us to do in this case can be found here
|
|
110
151
|
https://react.dev/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes.
|
|
111
|
-
|
|
152
|
+
The above example was changed to match the docs. It still rerenders the ChildComponent twice, which still causes a smaller
|
|
112
153
|
performance penalty, is hard to reason about and works only for states defined in the same component
|
|
113
|
-
(if syncState was a prop, it wouldn't work).
|
|
154
|
+
(if syncState was a prop, it wouldn't work). Also, the parentState and syncState will be synchronized only starting with
|
|
155
|
+
the second render.
|
|
114
156
|
|
|
115
|
-
```typescript
|
|
157
|
+
```typescript jsx
|
|
116
158
|
function ChildComponent({ parentState }: { parentState: number }) {
|
|
117
159
|
const [syncState, setSyncState] = useState(parentState);
|
|
118
160
|
|
|
@@ -126,12 +168,13 @@ function ChildComponent({ parentState }: { parentState: number }) {
|
|
|
126
168
|
}
|
|
127
169
|
```
|
|
128
170
|
|
|
129
|
-
The 'use-synchronized-state' hook fixes
|
|
171
|
+
The 'use-synchronized-state' hook fixes some of these issues by synchronizing the values in an easier and more consistent manner. In the bellow example,
|
|
130
172
|
if the reactive value changes (for example setParentState(1) is called), both parentState and syncState will have
|
|
131
|
-
the same value from the first render (after the setParentState)
|
|
173
|
+
the same value from the first render (after the setParentState). There will still be two renders, but the values will be in sync from the first one.
|
|
174
|
+
Also, the api is easy to use and it works for any reactive value.
|
|
132
175
|
|
|
133
176
|
|
|
134
|
-
```typescript
|
|
177
|
+
```typescript jsx
|
|
135
178
|
import { useSyncState } from 'use-synchronized-state';
|
|
136
179
|
|
|
137
180
|
function ChildComponent({ parentState }: { parentState: number }) {
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var react=require("react");function useSyncState(e){var t=react.useState(
|
|
1
|
+
var react=require("react");function useSyncState(e){var t=react.useState(e),a=t[0],t=t[1],r=react.useState(e),c=r[0],r=r[1];return a!==e&&(t(c=e),r(e)),[c,r]}exports.useSyncState=useSyncState;
|
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,6 @@ import { Dispatch, SetStateAction } from 'react';
|
|
|
5
5
|
* the returned state changes to the same value,
|
|
6
6
|
* but it can also change independently when the returned setState is called.
|
|
7
7
|
* @param propsState - a reactive value (e.g. state, props) with which our returned state will synchronize
|
|
8
|
-
*
|
|
8
|
+
* @return the exact same output as useState: [state, setState], where state is the synchronized state with the propsState
|
|
9
9
|
*/
|
|
10
10
|
export declare function useSyncState<T>(propsState: T): [T, Dispatch<SetStateAction<T>>];
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{useState
|
|
1
|
+
import{useState}from"react";function useSyncState(t){var e=useState(t),a=e[0],e=e[1],r=useState(t),u=r[0],r=r[1];return a!==t&&(e(u=t),r(t)),[u,r]}export{useSyncState};
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "use-synchronized-state",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "A React hook that creates a synchronized state with a reactive value
|
|
5
|
-
"homepage": "https://github.com/rhorge/use-
|
|
3
|
+
"version": "1.0.16",
|
|
4
|
+
"description": "A React hook that creates a synchronized state with a reactive value (fixing the Cascading updates issue)",
|
|
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",
|