React HooksEasy
What is useFormStatus in React 19?
useFormStatus is a hook that gives you deep visibility into the status of the parent <form> it is rendered within. It is specifically designed to work seamlessly with React 19's new form actions.
The Core Benefit:
In the past, if you wanted a deeply nested submit button to know if the form was currently loading so it could show a spinner or disable itself, you had to pass an isLoading prop all the way down the tree (prop drilling) or use a custom Context.
useFormStatus reads the form status natively from the DOM context. You simply call it inside any component that is rendered inside a form, and it provides:
pending: Boolean indicating if the form is currently submitting.data: The FormData object currently being submitted.method: The HTTP method (e.g., 'post' or 'get').action: A reference to the action function being executed.
Code example
tsx
1import React, { useFormStatus } from 'react';23// 1. A deeply nested UI component4// Notice it does NOT take any props related to loading state!5function SubmitButton() {6 // 🚀 Reads the pending state automatically from the nearest parent <form>7 const { pending } = useFormStatus();89 return (10 <button11 type="submit"12 disabled={pending}13 className="bg-indigo-600 text-white px-6 py-2 rounded-xl font-bold shadow disabled:bg-indigo-300 transition-all"14 >15 {pending ? (16 <span className="flex items-center gap-2">17 <div className="w-4 h-4 border-2 border-white border-t-transparent rounded-full animate-spin" />18 Saving...19 </span>20 ) : 'Save Changes'}21 </button>22 );23}2425// Simulated submission action26async function submitAction(formData) {27 await new Promise(r => setTimeout(r, 2000));28 console.log("Saved:", formData.get('username'));29}3031export default function ProfileForm() {32 return (33 <div className="p-8 border rounded-2xl max-w-sm">34 <h2 className="text-xl font-bold mb-4">Edit Profile</h2>3536 {/* 2. The Parent Form */}37 <form action={submitAction} className="space-y-4">38 <input39 type="text"40 name="username"41 placeholder="Username"42 className="w-full p-2 border rounded-lg"43 />4445 {/* The child button will automatically know when this form is submitting */}46 <SubmitButton />47 </form>48 </div>49 );50}
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React Hooks set.