Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 3201

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T12:33:07+00:00 2024-11-26T12:33:07+00:00

useFormStatus in React 19

  • 61k

Hello everyone. I would like to write about useFormStatus updated in React 19, which is one of new hooks. This hook is relevant to Form Actions. Hence, If you do not understand Form Actions, please read the previous post before reading this article.

What is useFormStatus?

“useFormStatus” is new hook obtain the status during form submission. Using this hook, you can display the status of the form on the screen, or restrict form input during submission.

Instructions

①Create a form using Form Actions
/app/page.tsx

  export default function Home() {   const formAction = async (formData: FormData) => {     "use server";     await new Promise((resolve) => {       setTimeout(resolve, 1000);     }); //intentionally delay time     console.log(formData.get("firstName"));   };    return (     <form action={formAction}>       <div>         <label>Name:</label>         <input type="text" name="firstName" />       </div>       <button className="border border-black">Resister</button>     </form>   ); }   
Enter fullscreen mode Exit fullscreen mode

The feature of this form is following description.

  • When you click the registration button on the form, the entered name will be output to the debug console log as server-side processing.

  • Because I want to check the status of the form, I intentionally delay the process by using PROMISE and SET TIMEOUT.

②Convert the button into a component
/app/Register.tsx

  const Register = () => {   return <button className="border border-black">Resister</button>; };  export default Register;   
Enter fullscreen mode Exit fullscreen mode

/app/page.tsx

  import Register from "./Register";  export default function Home() {   const formAction = async (formData: FormData) => {     "use server";     await new Promise((resolve) => {       setTimeout(resolve, 1000);     }); //intentionally delay time     console.log(formData.get("firstName"));   };    return (     <form action={formAction}>       <div>         <label>Name:</label>         <input type="text" name="firstName" />       </div>       <Register />     </form>   ); }   
Enter fullscreen mode Exit fullscreen mode

The element(submission button) under the form that use server action, which is converted into child component. In this case, I converted button element into Register.tsx.

③Use the useFormStatus
/app/Register.tsx

  "use client"; import { useFormStatus } from "react-dom";  const Register = () => {   const { pending } = useFormStatus();    return (     <button className="border border-black">       {pending ? "Loading" : "Register"}     </button>   ); };  export default Register;   
Enter fullscreen mode Exit fullscreen mode

I added following in the Register.tsx.

  • “const { pending } = useFormStatus();” is to get the status of form submission. The text of the button is changed by changing the content of the variable “pending”.

  • I added “use client”;”

④Result
Image description
I can see the Loading during submission of the form.

Thanks for reading this article.
Next time, I will explain a new hook, which is useFormState.

nodereactreact19webdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.