Design Converter
Education
Last updated on Mar 20, 2025
•5 mins read
Last updated on Mar 20, 2025
•5 mins read
The useFormStatus
hook is a game-changer for form management in React apps. It allows you to track the status of form submissions by returning a status object with useful properties such as pending
, data
, and method
. This hook makes it easier to handle form submission states, including tracking the HTTP method used, the data submitted, and whether the form is in a pending state.
In this blog, we will dive into the basic usage of the useFormStatus
hook, cover advanced topics like handling server actions, error messages, and integrating it with React components.
useFormStatus
Hook?The useFormStatus
hook is designed to track the status of a form submission. It provides status information for the specific form it is attached to, such as whether there is an active or pending submission. This hook is typically used inside a <form>
element, where the action
attribute (or action
prop) on the form component defines the form action endpoint.
The status object returned by the hook includes:
pending: A Boolean value indicating if the form submission is active (i.e., an active submission is in progress). This property is essential for disabling the submit button or displaying a loading indicator.
data: Contains the form data (this is initially null until the user submits the form). This data includes values from various input type fields.
method: A string value that represents the HTTP method used during the submission (either GET, POST, etc.).
useFormStatus
useFormStatus
Hook PropertiesProperty | Description | Example Usage |
---|---|---|
pending | Indicates if the form submission is currently in progress | disabled={pending} on the submit button |
data | Contains the form data submitted by the user | JSON.stringify(data) to display the data |
method | A string value showing the HTTP method (e.g., POST, GET) used for the form action | Display the method in the UI |
useFormStatus
in a Form ComponentHere is an example of a simple form component that uses the useFormStatus
hook to manage form submission states.
1import React from 'react'; 2import { useFormStatus } from 'your-form-library'; // Import the hook 3 4export default function App() { 5 const { pending, data, method } = useFormStatus(); // Get status information from the hook 6 7 async function submit(e) { 8 e.preventDefault(); 9 try { 10 // Simulate a form submission with a promise 11 const response = await new Promise((resolve) => 12 setTimeout(() => resolve({ status: 'success', response: 'OK' }), 2000) 13 ); 14 console.log('Submission response:', response); 15 } catch (error) { 16 console.error('Submission failed:', error); 17 } 18 } 19 20 return ( 21 <form onSubmit={submit} action="/submit" method="POST"> 22 <div className="form-fields"> 23 <input type="text" name="username" placeholder="Enter username" required /> 24 <input type="email" name="email" placeholder="Enter email" required /> 25 </div> 26 <button type="submit" disabled={pending}> 27 {pending ? 'Loading...' : 'Submit'} {/* button text changes based on pending state */} 28 </button> 29 {data && ( 30 <div className="submission-summary"> 31 <h4>Submission Details</h4> 32 <pre>{JSON.stringify(data, null, 2)}</pre> 33 <p>Method used: {method}</p> 34 </div> 35 )} 36 {/* Optionally, display error messages */} 37 {!pending && false && ( 38 <div className="error-messages"> 39 <p>Error: Please check your input and try again.</p> 40 </div> 41 )} 42 </form> 43 ); 44}
useFormStatus
hook provides the status (pending
, data
, and method
) for form submission.submit
function handles the form submission and simulates a promise to represent a server action.Loading...
vs. Submit
).useFormStatus
with Other Hooks and ComponentsIn more advanced scenarios, you may need to coordinate useFormStatus
with other hooks to manage complex form states. For example, you can pass the form state to child components or use multiple hooks together to handle form actions and field updates.
1import React, { useState } from 'react'; 2import { useFormStatus, useActionStateHook } from 'your-form-library'; 3 4export function AdvancedForm() { 5 const { pending, data, method } = useFormStatus(); 6 const [state, setState] = useActionStateHook({ initialValue: {} }); 7 8 async function submit(e) { 9 e.preventDefault(); 10 setState({ ...state, formData: e.target.elements }); 11 try { 12 const response = await new Promise((resolve) => setTimeout(() => resolve({ status: 'success' }), 1500)); 13 console.log('Submission response:', response); 14 } catch (error) { 15 console.error('Submission error:', error); 16 } 17 } 18 19 return ( 20 <form onSubmit={submit} action="/advanced-submit" method="POST"> 21 <input type="text" name="fullname" placeholder="Full Name" required /> 22 <input type="text" name="uri" placeholder="URI Value" required /> 23 <button type="submit" disabled={pending}> 24 {pending ? 'Submitting...' : 'Submit'} 25 </button> 26 {data && ( 27 <div className="submission-feedback"> 28 <p>Submitted using method: {method}</p> 29 <pre>{JSON.stringify(data, null, 2)}</pre> 30 </div> 31 )} 32 </form> 33 ); 34}
AdvancedForm
component demonstrates using both useFormStatus
and useActionStateHook
to manage form submission.useActionStateHook
, and submission is handled by the submit
function.useFormStatus
Using the useFormStatus
hook simplifies form management in React by handling submission states, errors, and server actions. By using it in combination with other hooks, you can easily manage complex form workflows.
This guide has shown you how to:
useFormStatus
in your form components.pending
, data
, method
) to update the UI.With these concepts and examples, you can take full advantage of the useFormStatus
hook to build powerful and efficient forms in your React applications.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.