Skip to content

Commit f11f0b4

Browse files
Refs And Memoization: Rephrase sentence to clarify useMemo behavior (#30939)
1 parent 08d025d commit f11f0b4

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

react/more_react_concepts/refs_and_memoization.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ function Cart({ products }) {
134134
}
135135
```
136136

137-
In the example above, we can easily memoize the calculated value by wrapping it in a `useMemo`, as the syntax is pretty much the same as `useEffect` and almost works the same. Where `useMemo` will also *execute* the callback on mount, and on subsequent re-renders, it will only *re-execute* the callback whenever one of the dependencies *changes*. In our case, whenever the `products` prop changes.
137+
In the example above, we can easily memoize the calculated value by wrapping it in a `useMemo`, as the syntax is pretty much the same as `useEffect` and almost works the same. `useMemo` will run the callback on mount. On subsequent re-renders, it will only re-run the callback if a dependency changes, otherwise it will return the cached value from whenever it was last run. In our case, whenever the `products` prop changes.
138138

139139
This way, whenever a user opens/closes the cart multiple times, it will not recalculate the `totalPrice` and use the cached value as long as `products` did not change.
140140

0 commit comments

Comments
 (0)