use-synchronized-state 1.0.5 → 1.0.6

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.
Files changed (2) hide show
  1. package/README.md +17 -21
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -27,7 +27,7 @@ import { useSyncState } from 'use-synchronized-state';
27
27
 
28
28
  function ParentComponent() {
29
29
  const [parentState, setParentState] = useState(0);
30
-
30
+
31
31
  return (
32
32
  <ChildComponent parentState={parentState} />
33
33
  );
@@ -41,12 +41,12 @@ function ChildComponent({ parentState }: { parentState: number }) {
41
41
  ```
42
42
 
43
43
  ## Description
44
- This hook creates a synchronized state while avoiding the cascading updates issue.
44
+ This hook creates a synchronized state while avoiding the cascading updates issue.
45
45
  In our case, synchronized means:
46
46
  - When there is a change in the reactive value that we synchronize our state with,
47
47
  the returned state changes to the same value, but it can also change independently when the returned setState is called.
48
48
 
49
-
49
+
50
50
  ### Let's see a small example
51
51
 
52
52
  ```typescript
@@ -67,17 +67,17 @@ function ChildComponent({ parentState }: { parentState: number }) {
67
67
  ```
68
68
 
69
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).
70
+ (passing its state as props).
71
71
 
72
- When the child component mounts (is rendered for the first time), the unsyncState state has the same
72
+ When the child component mounts (is rendered for the first time), the unsyncState state has the same
73
73
  value as the parentState prop (or the parentState state, which is passed as a prop).
74
74
 
75
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).
76
+ but the value of the unsyncState will not (because the state is only initialized when the component mounts).
77
77
 
78
78
  What can we do in the following situation?
79
79
 
80
- The first thing that comes to mind is to do something like this:
80
+ The first thing that comes to mind is to do something like this:
81
81
 
82
82
  ```typescript
83
83
  function ChildComponent({ parentState }: { parentState: number }) {
@@ -90,26 +90,26 @@ function ChildComponent({ parentState }: { parentState: number }) {
90
90
  }
91
91
  ```
92
92
 
93
- This is doing its job, but we now have another problem called "cascading updates" (which will be spotted by the
93
+ This is doing its job, but we now have another problem called "cascading updates" (which will be spotted by the
94
94
  React Performance tracks). You can read the docs at the following link
95
95
  https://react.dev/reference/dev-tools/react-performance-tracks#cascading-updates.
96
96
 
97
97
  To explain why this happens, we first have to know that React Fiber algorithm has two phases: the render phase and
98
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.
99
+ virtual dom is rendered.
100
100
 
101
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
102
+ (the initial value of parentState). Setting parentState to a new value (let's say 1), React will render the
103
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
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
106
  (having also a performance penalty).
107
107
 
108
108
 
109
- What react docs tell us to do in this case can be found here
109
+ What react docs tell us to do in this case can be found here
110
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
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
113
  (if syncSate was a prop, it wouldn't work).
114
114
 
115
115
  ```typescript
@@ -126,7 +126,7 @@ function ChildComponent({ parentState }: { parentState: number }) {
126
126
  }
127
127
  ```
128
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,
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
130
  if the reactive value changes (for example setParentState(1) is called), both parentState and syncState will have
131
131
  the same value from the first render (after the setParentState) and there will be no additional render (only the needed one).
132
132
 
@@ -141,8 +141,4 @@ function ChildComponent({ parentState }: { parentState: number }) {
141
141
  ```
142
142
 
143
143
  ## License
144
- MIT
145
-
146
-
147
-
148
-
144
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "use-synchronized-state",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "A React hook that creates a synchronized state with a reactive value in react (fixing the Cascading updates issue)",
5
5
  "author": "rhorge",
6
6
  "license": "MIT",