RFC: Introducing a new hook useDeferredState #251
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This proposal suggests simplifying the usage of
startTransition
inReact
byintroducing a new hook.
Basic example
Currently, in order to utilize
startTransition
, developers need toimport
useTransition
and set it up as follows:However, in some cases, the
isPending
value is not used, but it still needs to be included becausestartTransition
occupies the second index of theuseTransition
output. Additionally,startTransition
often needs to work in conjunction withuseState
.To address these complexities, I propose combining these hooks into a new one named
useDeferredState
Alternatively, we can achieve the same result using a HOF to wrap
useState
, like this:Motivation
The main motivation behind this proposal is to streamline the usage of
useTransition
andstartTransition
inReact
, making it more straightforward and intuitive for developers. By introducing a new hook or a HOF, we can enhance the developer experience and promote cleaner, more concise code when working with transitions in React.Detailed design
useDeferredState
anddeferred
are a combination of two main React hooksuseState
anduseTransition
.By placing
isPending
as the third element in the output, it provides the flexibility to choose whether or not to utilize it in your code:Drawbacks
useTransition
is much harder than teachinguseDeferredState
Alternatives
An alternative solution is having
useDeferredState
ordeferred
asa 3th-party package.
or
Adoption strategy
The proposed hook and Higher-Order Function are opt-in features, meaning that developers can
choose whether or not to use them in their code. Existing React applications that don't require
the new functionalities can continue using the traditional
useState
anduseTransition
approacheswithout any changes.
How we teach this
useDeferredState
:The proposed hook combines
useState
with a deferred state update mechanism to handle non-urgent updates. The termdeferred
accurately describes the behavior of the state update, as it delays execution until a later time (when React is less busy). This name aligns with React's existing hooks, such asuseEffect
, which also involve deferred operations.deferred
:When introducing the Higher-Order Function, it wraps useState to achieve similar functionality to the
useDeferredState
hook. The namedeferred
directly reflects the primary purpose of the function, emphasizing its role in handling state updates in a deferred manner.Unresolved questions
Is there any downside if we use multiple
useTransition
in a component?