Skip to content

Commit

Permalink
Tweak useMemo wording (#1569)
Browse files Browse the repository at this point in the history
* Tweak useMemo wording

* Update hooks-faq.md

* Update hooks-reference.md

* Be more specific
  • Loading branch information
gaearon committed Jan 17, 2019
1 parent 1a49812 commit 9b5638f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions content/docs/hooks-faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ See also [the recommended pattern for derived state](#how-do-i-implement-getderi

### How do I implement `getDerivedStateFromProps`?

While you probably [don't need it](/blog/2018/06/07/you-probably-dont-need-derived-state.html), for the rare cases that you do (such as implementing a `<Transition>` component), you can update the state right during rendering. React will re-run the component with updated state immediately after exiting the first render so it wouldn't be expensive.
While you probably [don't need it](/blog/2018/06/07/you-probably-dont-need-derived-state.html), in rare cases that you do (such as implementing a `<Transition>` component), you can update the state right during rendering. React will re-run the component with updated state immediately after exiting the first render so it wouldn't be expensive.

Here, we store the previous value of the `row` prop in a state variable so that we can compare:

Expand Down Expand Up @@ -343,7 +343,7 @@ const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);

This code calls `computeExpensiveValue(a, b)`. But if the inputs `[a, b]` haven't changed since the last value, `useMemo` skips calling it a second time and simply reuses the last value it returned.

`useMemo` is treated as a hint rather than guarantee. React may still choose to "forget" some previously memoized values to free memory, and recalculate them on next render.
**You may rely on `useMemo` as a performance optimization, not as a semantic guarantee.** In the future, React may choose to "forget" some previously memoized values and recalculate them on next render, e.g. to free memory for offscreen components. Write your code so that it still works without `useMemo`and then add it to optimize performance. (For rare cases when a value must *never* recomputed, you can [lazily initialize](#how-to-create-expensive-objects-lazily) a ref.)

Conveniently, `useMemo` also lets you skip an expensive re-render of a child:

Expand Down
4 changes: 2 additions & 2 deletions content/docs/hooks-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ Pass a "create" function and an array of inputs. `useMemo` will only recompute t

If no array is provided, a new value will be computed whenever a new function instance is passed as the first argument. (With an inline function, on every render.)

**Don't rely on `useMemo` for correctness.** React treats it as an optimization hint and does not *guarantee* to retain the memoized value. For example, React may choose to "forget" some previously memoized values to free memory, and recalculate them on next render.
**You may rely on `useMemo` as a performance optimization, not as a semantic guarantee.** In the future, React may choose to "forget" some previously memoized values and recalculate them on next render, e.g. to free memory for offscreen components. Write your code so that it still works without `useMemo`and then add it to optimize performance.

> Note
>
Expand Down Expand Up @@ -383,4 +383,4 @@ function useFriendStatus(friendID) {

> Tip
>
> We don't recommend adding debug values to every custom hook. It's most valuable for custom hooks that are part of shared libraries.
> We don't recommend adding debug values to every custom hook. It's most valuable for custom hooks that are part of shared libraries.

0 comments on commit 9b5638f

Please sign in to comment.