Hello Developers π,
Itβs me your friend Md Taqui Imam, and today Iβm going to explain a new and exciting hook in React called useActionState.
What is useActionState?
useActionState is a new React hook that helps us update state based on the result of a form action.
Itβs like a smart helper that remembers things for us and can change them when we submit a form.
Checkout Official Documentationπ
Important Note: Right now, useActionState is only available in Reactβs Canary and experimental channels. To get the most out of it, youβll need to use a framework that supports React Server Components.
How to use useActionState?
To use this hook, we first need to import it from React:
import { useActionState } from 'react';
Then, we can use it in our component like this:
const [state, formAction, isPending] = useActionState(actionFunction, initialState);
Hereβs what each part means:
βstateβ is our current form state
βformActionβ is a new action weβll use in our form
βactionFunctionβ is the function that runs when the form is submitted
βinitialStateβ is the starting value of our state
When to use useActionState:
Use this hook when you want to update state based on form submissions, especially if youβre using Server Components and want quicker responses.
Example:
Letβs make a simple counter form using useActionState:
import { useActionState } from "react";
async function increment(previousState, formData) {
return previousState + 1;
}
function StatefulForm() {
const [state, formAction] = useActionState(increment, 0);
return (
<form>
{state}
<button formAction={formAction}>Increment</button>
</form>
);
}
In this example, every time we click the button, our count goes up by one. The useActionState hook takes care of updating the state when the form is submitted.
For More Detail and example checkout this video π
Thatβs it π
Remember, the best way to learn is by doing.
So when useActionState becomes more widely available, give it a try in your projects and see how it can improve your forms!
Alert β οΈ
Don't forget to checkout my new article π«‘
...
Top comments (25)
It is definitely nice to use server actions and useActionState, but I still like more control over validation and still prefer Formik + Yup combo. To be honest, the end user doesn't care at the end and cannot tell the difference π¨πΌβπ»
Thanks for you suggestions π«‘
What's the difference between useactionstate and use effect
Both useActionState and useEffect are hooks in React, but they serve different purposes and are used in different scenarios
Just like salt and mackerel. They both are edible "things" used in different scenarios π
What I really meant is...
that we should get back the "get a life" trend but with people that use AI for everything while trying to hide it. Stop it. It's sad. If that wasn't enough its even more easy to spot AI generated text than it is to spot SEO directed content written by humans.
ππ too good
useActionState : For Form Mutations.
useEffect : lifecycle hook to control components state
π
So React keeps on piling up things on top of things. Poor React devs, having to keep up with this hot mess.
π π
I cant get the message from useActionState to return the server action value, see example
server file :
client file :
Interesting topic! Everything is explained articulately and clearly. For your project, consider checking out this free npm package: select-paginated.
Great article Taqui. I enjoyed learning from it
Thanks Lucian π
π
Thanks for Sharing
Happy to hear you liked it π₯°
nice share πππ»
Happy to hear that you liked it π
Some comments may only be visible to logged-in visitors. Sign in to view all comments.