use-synchronized-state 1.0.0
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/LICENSE +21 -0
- package/README.md +148 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.mjs +1 -0
- package/package.json +74 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 rhorge
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# A React hook that creates a synchronized state with a reactive value in react (fixing the Cascading updates issue)
|
|
2
|
+
|
|
3
|
+
## Highlights
|
|
4
|
+
- Offers a hook called useSyncState
|
|
5
|
+
- Removes the cascading update problem
|
|
6
|
+
- Same api as useState
|
|
7
|
+
- No extra dependencies
|
|
8
|
+
- Written in react with typescript
|
|
9
|
+
- Typescript support
|
|
10
|
+
- Small bundle size
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
yarn add use-synchronized-state
|
|
16
|
+
```
|
|
17
|
+
or
|
|
18
|
+
```sh
|
|
19
|
+
npm install use-synchronized-state
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { Dispatch, SetStateAction, useState } from 'react';
|
|
26
|
+
import { useSyncState } from 'use-synchronized-state';
|
|
27
|
+
|
|
28
|
+
function ParentComponent() {
|
|
29
|
+
const [parentState, setParentState] = useState(0);
|
|
30
|
+
|
|
31
|
+
return (
|
|
32
|
+
<ChildComponent parentState={parentState} />
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function ChildComponent({ parentState }: { parentState: number }) {
|
|
37
|
+
// this syncState is synchronized with parentState (has the same api as useState)
|
|
38
|
+
const [syncState, setSyncState] = useSyncState(parentState);
|
|
39
|
+
// ...
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Description
|
|
44
|
+
This hook creates a synchronized state while avoiding the cascading updates issue.
|
|
45
|
+
In our case, synchronized means:
|
|
46
|
+
- When there is a change in the reactive value that we synchronize our state with,
|
|
47
|
+
the returned state changes to the same value, but it can also change independently when the returned setState is called.
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
### Let's see a small example
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
import { Dispatch, SetStateAction, useState } from 'react';
|
|
54
|
+
|
|
55
|
+
function ParentComponent() {
|
|
56
|
+
const [parentState, setParentState] = useState(0);
|
|
57
|
+
|
|
58
|
+
return (
|
|
59
|
+
<ChildComponent parentState={parentState} />
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function ChildComponent({ parentState }: { parentState: number }) {
|
|
64
|
+
const [unsyncState, setUnsyncState] = useState(parentState);
|
|
65
|
+
// ...
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
In the above example, we have a simple component structure: a parent component that renders a child component
|
|
70
|
+
(passing its state and setter function).
|
|
71
|
+
|
|
72
|
+
When the child component mounts (is rendered for the first time), the unsyncState state have the same
|
|
73
|
+
value as the parentState prop (or the parentState state, which is passed as a prop).
|
|
74
|
+
|
|
75
|
+
Now if the parentState changes (by calling its setParentState setter), the parentState prop will change accordingly,
|
|
76
|
+
but the value of the unsyncState will not (because the state is only initialized when the component mounts).
|
|
77
|
+
|
|
78
|
+
What can we do in the following situation?
|
|
79
|
+
|
|
80
|
+
The first thing that comes to mind is to do something like this:
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
function ChildComponent({ parentState }: { parentState: number }) {
|
|
84
|
+
const [syncState, setSyncState] = useState(parentState);
|
|
85
|
+
|
|
86
|
+
useEffect(() => {
|
|
87
|
+
setSyncState(parentState)
|
|
88
|
+
}, [parentState]);
|
|
89
|
+
// ...
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
This is doing its job, but we now have another problem called "cascading updates" (which will be spotted by the
|
|
94
|
+
React Performance tracks). You can read the docs at the following link
|
|
95
|
+
https://react.dev/reference/dev-tools/react-performance-tracks#cascading-updates.
|
|
96
|
+
|
|
97
|
+
To explain why this happens, we first have to know that React fiber algorithm has two phases: the render phase and
|
|
98
|
+
the commit phase. All you need to know, to understand the issue, is that React will call useEffect after the entire
|
|
99
|
+
virtual dom is rendered.
|
|
100
|
+
|
|
101
|
+
So, in our case, after the first render will complete parentState and syncState will have both the value 0
|
|
102
|
+
(the initial value of parentState). Setting parentState to a new value (let's say 1), React will render the
|
|
103
|
+
ChildComponent with the parentState props as 1, but the syncState will still have the previous value 0.
|
|
104
|
+
Once the useEffect runs, setting the syncState to 1, we will have another render that has both values equal to 1.
|
|
105
|
+
This is the cascading updates problem in all its glory, instead of having one render, you ended up having two
|
|
106
|
+
(having also a performance penalty).
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
What react docs tell us to do in this case can be found here
|
|
110
|
+
https://react.dev/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes.
|
|
111
|
+
I have changed the above example to match it. It still rerenders the ChildComponent twice, which still causes a
|
|
112
|
+
performance penalty, is hard to reason about and works only for states defined in the same component
|
|
113
|
+
(if syncSate was a prop, it wouldn't work).
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
function ChildComponent({ parentState }: { parentState: number }) {
|
|
117
|
+
const [syncState, setSyncState] = useState(parentState);
|
|
118
|
+
|
|
119
|
+
// Better: Adjust the state while rendering
|
|
120
|
+
const [prevState, setPrevState] = useState(parentState);
|
|
121
|
+
if (items !== prevItems) {
|
|
122
|
+
setPrevState(parentState);
|
|
123
|
+
setSyncState(parentState);
|
|
124
|
+
}
|
|
125
|
+
// ...
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
The 'use-synchronized-state' hook fixes all these issues by synchronizing the values in the same and only render. In the bellow example,
|
|
130
|
+
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) and there will be no additional render (only the needed one).
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
import { useSyncState } from 'use-synchronized-state';
|
|
136
|
+
|
|
137
|
+
function ChildComponent({ parentState }: { parentState: number }) {
|
|
138
|
+
const [syncState, setSyncState] = useSyncState(parentState);
|
|
139
|
+
// ...
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## License
|
|
144
|
+
MIT
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var react=require("react");function useSyncState(e){var r=react.useState(e)[1],t=react.useRef(e),c=react.useRef(e),c=(c.current!==e&&(c.current=e,t.current=e),react.useCallback(function(e){t.current=e instanceof Function?e(t.current):e,r(t.current)},[t]));return[t.current,c]}exports.useSyncState=useSyncState;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Dispatch, SetStateAction } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* This hook creates a synchronized state while avoiding the cascading update issue.
|
|
4
|
+
* In our case, synchronized means that when there is a change in propsState,
|
|
5
|
+
* the returned state changes to the same value,
|
|
6
|
+
* but it can also change independently when the returned setState is called.
|
|
7
|
+
* @param propsState - a reactive value (e.g. state, props) with which our returned state will synchronize
|
|
8
|
+
* Returns the exact same output as useState: [state, setState], where state is the synchronized state with the propsState
|
|
9
|
+
*/
|
|
10
|
+
export declare function useSyncState<T>(propsState: T): [T, Dispatch<SetStateAction<T>>];
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{useState,useRef,useCallback}from"react";function useSyncState(e){var t=useState(e)[1],r=useRef(e),u=useRef(e),u=(u.current!==e&&(u.current=e,r.current=e),useCallback(function(e){r.current=e instanceof Function?e(r.current):e,t(r.current)},[r]));return[r.current,u]}export{useSyncState};
|
package/package.json
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "use-synchronized-state",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A React hook that creates a synchronized state with a reactive value in react (fixing the Cascading updates issue)",
|
|
5
|
+
"author": "rhorge",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": "https://github.com/rhorge/use-synchronized-state",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "dist/index.cjs",
|
|
10
|
+
"module": "dist/index.mjs",
|
|
11
|
+
"exports": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"require": "./dist/index.cjs",
|
|
14
|
+
"import": "./dist/index.mjs"
|
|
15
|
+
},
|
|
16
|
+
"types": "dist/index.d.ts",
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=8",
|
|
19
|
+
"npm": ">=5"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "rollup -c",
|
|
23
|
+
"start": "rollup -c -w",
|
|
24
|
+
"test": "jest"
|
|
25
|
+
},
|
|
26
|
+
"jest": {
|
|
27
|
+
"transform": {
|
|
28
|
+
".(ts|tsx)": "ts-jest"
|
|
29
|
+
},
|
|
30
|
+
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
|
|
31
|
+
"moduleFileExtensions": [
|
|
32
|
+
"ts",
|
|
33
|
+
"tsx",
|
|
34
|
+
"js"
|
|
35
|
+
],
|
|
36
|
+
"modulePathIgnorePatterns": [
|
|
37
|
+
"<rootDir>/dist/"
|
|
38
|
+
]
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"react": ">= 16.8.0",
|
|
42
|
+
"react-dom": ">= 16.8.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@testing-library/react-hooks": "^3.4.1",
|
|
46
|
+
"@types/jest": "^26.0.7",
|
|
47
|
+
"@types/react": "^16.3.13",
|
|
48
|
+
"@types/react-dom": "^16.0.5",
|
|
49
|
+
"babel-core": "^6.26.3",
|
|
50
|
+
"babel-runtime": "^6.26.0",
|
|
51
|
+
"eslint-plugin-react-hooks": "^4.0.8",
|
|
52
|
+
"jest": "^26.1.0",
|
|
53
|
+
"react": "^16.12.0",
|
|
54
|
+
"react-dom": "^16.12.0",
|
|
55
|
+
"react-test-renderer": "^16.13.1",
|
|
56
|
+
"rollup": "^4.22.4",
|
|
57
|
+
"rollup-plugin-typescript2": "^0.25.3",
|
|
58
|
+
"rollup-plugin-uglify": "^6.0.4",
|
|
59
|
+
"ts-jest": "^26.1.3",
|
|
60
|
+
"typescript": "^3.8.3"
|
|
61
|
+
},
|
|
62
|
+
"files": [
|
|
63
|
+
"dist/*",
|
|
64
|
+
"README.md",
|
|
65
|
+
"LICENSE",
|
|
66
|
+
"package.json"
|
|
67
|
+
],
|
|
68
|
+
"keywords": [
|
|
69
|
+
"react",
|
|
70
|
+
"react hooks",
|
|
71
|
+
"typescript",
|
|
72
|
+
"npm"
|
|
73
|
+
]
|
|
74
|
+
}
|