React 19 is now stable! New Hooks
Hemantha Wijegunarathna
Software Engineer | React JS | Node JS | Typescript | PHP
useTransition
Previously, managing response statuses such as idle, pending, success, and fail required maintaining a dedicated state. However, with React 19, this is no longer necessary as React handles these states internally. To leverage this improvement, we can use the useTransition hook.
Parameters
No parameters
Returns
Returns an array with the below two items.
Before introduce the useTransition hook, we have to maintain the response manually. Let's understand this with the below example.
Here we are manually maintaining the API request status manually in the isPending state. Before we invoke the updateName function, we change the isPending state to true and once we get the response from the async function we are changing isPending state to false.
But we can easily manage the same procedure by using useTransition like the below. React 19 will update the pending status automatically for you.
When the async function is called, the transition immediately updates the isPending state to true, indicating that the operation is in progress. Once the async function completes and returns a response, the transition resets the isPending state to false, signalling that the operation has finished.
startTransition
The startTransition function is one of a returned value of useTransition hook. It let's you to update the transition.
Parameters
The function passed to startTransition is called an "Action". By convention, any callback called inside startTransition (such as a callback prop) should be named action or include the "Action" suffix.
Returns
startTransition does not return anything.
Additionally you can invoke multiple actions in nested startTransaction actions like the below.
The function passed to startTransition is called the "Action". You can update state and (optionally) perform side effects within an Action, and the work will be done in the background without blocking user interactions on the page. A Transition can include multiple Actions, and while a Transition is in progress, your UI stays responsive.
To give the user feedback about in-progress Transitions, to isPending state switches to true at the first call to startTransition, and stays true until all Actions complete and the final state is shown to the user.
useActionState
React 19 introduces new hook called useActionState that allows us to update state based on the result of a form action. This make the common cases easier for Actions.
This will invoke at the top level of our component to create component state that is updated when a form action is invoked.
Parameters
Returns
Returns an array with the below values.
Let's simplify this like the below.
useActionState was previously called ReactDOM.useFormState in the Canary releases, but in version 19 "useFormState" is deprecated.
useFormStatus
This hook gives the status information of the last form submission. It's common to write design components that need access to information about the <form> they're in, without drilling props down to the component.
We can achieve this behavior through the Context API as well.
Parameters
No parameters
Returns
Returns an object with the below properties.
useOptimistic
useOptimistic is a React Hook that lets you show a different state while an async action is underway. It accepts some state as an argument and returns a copy of that state that can be different during the duration of an async action such as a network request. You provide a function that takes the current state and the input to the action, and returns the optimistic state to be used while the action is pending.
Parameters
Returns
Returns an array with the below values.
Software Engineer
2 个月Useful tips