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
startTransitioninReactbyintroducing a new hook.
Basic example
Currently, in order to utilize
startTransition, developers need toimport
useTransitionand set it up as follows:However, in some cases, the
isPendingvalue is not used, but it still needs to be included becausestartTransitionoccupies the second index of theuseTransitionoutput. Additionally,startTransitionoften needs to work in conjunction withuseState.To address these complexities, I propose combining these hooks into a new one named
useDeferredStateAlternatively, 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
useTransitionandstartTransitioninReact, 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
useDeferredStateanddeferredare a combination of two main React hooksuseStateanduseTransition.By placing
isPendingas the third element in the output, it provides the flexibility to choose whether or not to utilize it in your code:Drawbacks
useTransitionis much harder than teachinguseDeferredStateAlternatives
An alternative solution is having
useDeferredStateordeferredasa 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
useStateanduseTransitionapproacheswithout any changes.
How we teach this
useDeferredState:The proposed hook combines
useStatewith a deferred state update mechanism to handle non-urgent updates. The termdeferredaccurately 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
useDeferredStatehook. The namedeferreddirectly 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
useTransitionin a component?