Skip to content

Commit 82168c6

Browse files
committed
react edit: The new value depends on the old value
1 parent d4d27b9 commit 82168c6

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

react/README.md

+18-8
Original file line numberDiff line numberDiff line change
@@ -787,33 +787,43 @@ Do not directly mutate/update arrays or objects.
787787
788788
### 🔷 The new value depends on the old value
789789
790-
The state value is not the up-to-date value all the time.
790+
The state value is not always the up-to-date value.
791791
792792
> This is because React batches state updates for performance reasons. So, it doesn't guarantee that the state has been updated immediately after calling `setState`.
793793
794-
> If you have a series of state updates happening quickly, like in a loop or due to user interactions, using the regular form may lead to unexpected behavior.
794+
> This batching mechanism helps reduce the number of re-renders and improves overall performance.
795+
796+
> If you have a series of state updates happening quickly, using the regular form may lead to unexpected behavior.
795797
796798
- The usual way:
797799
> ```jsx
798-
> const [counter, setCounter] = useSate(0);
800+
> const [counter, setCounter] = useState(0);
799801
>
800802
> const handleClick = () => {
801803
> setCounter(counter + 1);
804+
>
805+
> setCounter(counter + 1);
802806
> };
803807
> ```
808+
>
809+
> No matter what, this updates the state by `+1` every time you click, not `+2`. In `handleClick`, the `counter` isn't updated immediately, causing both `setCounter` calls to see the same `counter` value.
810+
>
811+
> `useState` doesn't trigger a component re-render if it detects that the new value is the same as the old value (applies to primitive values only). Hence, it will re-render the component only once.
804812
- The guaranteed way:
805813
> ```jsx
806-
> const [counter, setCounter] = useSate(0);
814+
> const [counter, setCounter] = useState(0);
807815
>
808816
> const handleClick = () => {
809-
> setCounter((currentCounter) => {
810-
> // `currentCounter` is the most up-to-date version of `counter`.
811-
> return currentCounter + 1;
817+
> setCounter((currVal) => {
818+
> // `currVal` is the most up-to-date version of `counter`.
819+
> return currVal + 1;
812820
> });
821+
>
822+
> setCounter((currVal) => currVal + 1);
813823
> };
814824
> ```
815825
>
816-
> Here, you provide a callback function that takes the current state as an argument. React ensures that this callback function is called with the most up-to-date state, even if multiple state updates are queued.
826+
> Here, you provide a callback function that takes the current state as an argument. React ensures that this callback function is invoked with the most up-to-date state, even if multiple state updates are queued. Consequently, the component will re-render twice.
817827
818828
<p align="right">
819829
<a href="#reactjs">back to top ⬆</a>

0 commit comments

Comments
 (0)