The useFormStatus Hook
Similarly to how we can get an action’s pending state through useActionState
, we can use useFormStatus
to get a form action’s pending state from a component that’s included in a form.
For example a submit button component. It’s common to be its own component, shared across different forms across the app, so this hook makes it easy to access its containing form’s pending state:
"use client"
import { useActionState } from "react"
import { myServerAction } from './actions'
const initialState = {
message: "",
}
export const Demo = () => {
const [state, formAction, pending] =
useActionState(myServerAction, initialState)
return (
<div>
<form action={formAction}>
<input
type='text'
name='fullName'
/>
{state?.message && <p>{state.message}</p>}
<SubmitButton />
</form>
</div>
)
}
const SubmitButton = () => {
const { pending } = useFormStatus()
return (
<button
aria-disabled={pending}
type='submit'>
{pending ? "Submitting..." : "Submit"}
</button>
)
}
→ Get my React Beginner's Handbook
I wrote 21 books to help you become a better developer:
- HTML Handbook
- Next.js Pages Router Handbook
- Alpine.js Handbook
- HTMX Handbook
- TypeScript Handbook
- React Handbook
- SQL Handbook
- Git Cheat Sheet
- Laravel Handbook
- Express Handbook
- Swift Handbook
- Go Handbook
- PHP Handbook
- Python Handbook
- Linux Commands Handbook
- C Handbook
- JavaScript Handbook
- Svelte Handbook
- CSS Handbook
- Node.js Handbook
- Vue Handbook
Also, JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025