New in 18: useDeferredValue #129
acdlite
announced in
Announcement
Replies: 1 comment 5 replies
-
Could you expand on this? Does it mean that in a future render |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
New in 18: useDeferredValue
When the value passed to useDeferredValue changes, it will either return the previous value or the new value depending on the priority of the current render.
You can think of "deferring" a value as similar to throttling or debouncing it.
If the current render is the result of an urgent update, like a user input, React will return the previous value. Then, it will switch to the new value after the current render has completed.
The deferred value has the same priority as updates scheduled by startTransition. Like startTransition updates, deferred values can suspend without triggering an unexpected fallback.
useDeferredValue and startTransition have broadly the same behavior. The difference lies mostly in where the API is applied:
To illustrate, take this example of a typeahead component:
In this example,
query
is a prop that's passed down from a parent component. We can't wrap the query update instartTransition
, since we don't control the event that changes it. There may even be multiple events — maybe the app listens both for input events and or a change to the URL. Or maybe the state is controlled by an external store.Memoize children to prevent re-renders during urgent updates
useDeferredValue only defers the value that you pass to it. If you want to prevent a child component from re-rendering during an urgent update, you must also memoize that component with memo or useMemo:
Memoizing the children tells React that it only needs to re-render them when
deferredQuery
changes. They do not need to re-render whenquery
changes.This is caveat is not unique to useDeferredValue, and it's the same pattern you would use with similar hooks like useThrottleValue or useDebouncedValue.
Beta Was this translation helpful? Give feedback.
All reactions