This New React Hook Changes How You Use Forms

Web Dev Simplified · Beginner ·🖊️ Copywriting & Content Strategy ·2y ago

Key Takeaways

The video demonstrates the use of React's experimental useFormStatus hook to simplify form handling, including creating loading states and managing form submissions. It also highlights the benefits of using this hook, such as reducing double form submissions and improving user experience.

Full Transcript

one of the most annoying things in react is dealing with form update loading states which is why I love the new use form status hook because it makes creating loading States like this so incredibly easy welcome back to web dev simplified my name is Kyle and my job is to simplify the web for you and in this video I'm going to show you how to fix this really common bug let's say that we're typing in some type of to do it's going to be just a new to do I click create it's kind of taking a while so I click it again and then oh it happened twice it loaded it two times essentially the API was taking so long that when I click create I got impatient and clicked it again and it caused it to actually send off multiple requests this is a common problem instead of applications that a lot of people don't plan for because they don't have slow enough connections or they're always working locally so they never have to worry about that slow connection speed so we'll take a look at the code here I can show you how to fix this the normal way and then I can show you how to fix this with the new use form status hook because that hooks makes it incredibly easy to fix and it makes it essentially automatic which is really nice so if we look at this code we're getting some initial to Do's just pretend that this is coming from some type of API we're setting that inside of our state and then what we're doing is whenever we make a submission we're creating a brand new to do inside of some type of API and we're returning that new to-do and setting that back into our state this create to-do function if we scroll down to it it's just a fake function normally like you'd call an API but essentially all it's doing is waiting one second and then returning a brand new to do with whatever our title is that we passed in then we're just rendering out that form and down here we're rendering out all of our to-do's this is really straightforward super simple code for the most part essentially the problem that we're having is while our to-do is being created right here while we're waiting for this we don't have any way to notify the user that something is happening so they just Spam that create button a bunch of times and as you can see a bunch of new to Do's get added to the list when you don't want them to so the normal way that you would handle this is by just throwing in some type of loading state so we can create a brand new state for like is loading set is loading and we can say by default this date is going to be false then what we can do before we make any request is we can set our is loading to true and then below that we can just set it back to false because now we are no longer loading and then we can use this data to essentially notify the user by for example disabling this button so we can disable it if we're in the loading State we can also render out different text so if we're loading we can like render out the text loading or something like that or we can render out the text to create so now if I try to type in it to do and I click create you can see it says loading the button is disabled so it doesn't matter how many times I click it only creates one single to do now this is great but it requires you to create this extra loading variable and if you have lots of different forms for example you may have lots of different loading States and honestly it's kind of difficult with naming to get them all named properly it's just not ideal it'd be nice that this was just kind of an automatic feature for you you could just hook into and that's where this use form status hook comes from now one thing I will copy out this video with is this is an experimental hook it's not in the stable version of react so to use this you need to make sure you're using an experimental version of react and react on as well as if you have the eslint plugin for the hooks you want to use the experimental version for that as well also if you're in typescript it's really important that you add these lines somewhere in your application it doesn't matter where I'm using V so I just threw them into this as well just to make sure you're using the experimental types as well now if you're using next JS and you're using server actions this is kind of where this whole idea for this hook came from so you can use this inside of next.js with the experimental server actions but I'm going to show you how you can use it in just plain react because it works in react as well so we want to import that hook and you may think we just import it up here by saying like use form status but this hook actually doesn't come from react it comes from react Dom which I know sounds a little bit crazy so we want to do this import from react Dom instead and we want to get that use form status hook and this is an experimental hook which is why it's prefixed with experimental I'm just going to rename it to use form status that way we can use it as if it was an actual hook that we can use in production so now we have this use form status hook now essentially the way that this works is it hooks into the action for your form but we need it to be inside of a custom component inside of our form so we need to have a custom component somewhere on our form and then we need to use this hook inside of it think about how you would use context in react your form is like the context and this use form status Hook is just consuming that context so it must be inside of your form and inside a custom component in there so we'll create a custom component for our submit button because that's the thing we care about modifying and what I'm going to do is I'm just going to copy this button return it down here just like that I'm going to get rid of all of this stuff up here and just return our submit button there we go and I'm going to get rid of all this loading State because we're actually going to handle this entirely with that use form status hook so now I'm going to use this hook inside of here so use form status this returns to us some data and if we look at this data you'll see that if we just say data dot that there are four different things we have the action we have the data we have the method and we have pending I'll talk about all these in just a second but the important one is pending this is essentially the same as that is loading variable so we'll just say that loading is equal to that pending status now if we just save and this is all we did you'll notice it still doesn't work we aren't actually loading our button this pending never sets to true and the reason for that is to actually use your form status you can't use the on submit function and instead you need to use the action function instead instead of a form in action normally takes a string to like the URL that you want to post to so we would say like new to do but inside of react you can also pass a function to this and this function will just call the function up here that we're using which is on submit just like that and this function is going to automatically cancel your event for you and instead of passing in an event object it's going to pass Us in some data and that data is going to be a form data just like that so this data it takes the form of this form data which is something that's built into HTML so now we don't even need a ref on our input because we already have that input value inside this form data and what we can do is we can just get our data from it so we can say here that our title is data.get and we want to get the title property and we just need to give our input a name of title to link them together so now these two things are linked and here we can just check to see if our title type is a string if it's not a string we're going to return otherwise we're going to pass it into here to create a brand new to do so we just had to minorly change how this function works but now that we're using an action instead of an on submit you'll notice when I try to create something I'm now getting that loading State automatically which is really nice and all the other data I'm getting as well so I can do a console.log just to show you what that data looks like I'm just going to type in a new to do click create and then we're going to take a look at that data if we go into the console you can see we have an object this is what this looks like if you don't have an action being currently performed you can see pending is false and everything is null if we do have an action being performed though you'll notice we have yet a bunch of information first of all our method this is a git request because we didn't specify a method on our form but if we change this to like a method of post you'll know that it'll say post instead also inside of here you'll see that we get our data that's the same exact form data passed to the on submit function up here so we can do whatever we want with that pending is set to true because it's currently loading and then we have an action which is just the function that we called so if we want we can call this exact same on submit function again now the way that this loading status works is as long as we're waiting for something so as long as this is a promise that is waiting to resolve that pending is going to be set to true so we need to make sure we're waiting for some type of Promise inside of here or you just return some type of Promise because it's depending on this being an asynchronous function that is going to do something and then once that thing is done it's then going to return to you that the pending is false so while this is doing something the pending value is going to be set to true so you'll notice that if I actually get rid of this await then you'll notice that it's no longer going to actually work because it's not waiting for anything so it doesn't know that you're in a pending status now I know the amount of code that you write for doing this versus the other way are pretty similar to be honest I mean there's not that much difference between the two ways but especially if you're working with something like next.js or something that already implements this form data using this system is so much easier and I really like that they're starting to add these nice features into react because it makes it a lot easier to do certain things because a lot of times you'd have to write all this boilerplate code for handling these things and having these custom hooks just makes it a little bit easier to do that now if this gets you excited to learn all about the newest react and next.js features I highly recommend checking out my brand new react simplified course it just launched last week and it covers everything you need to know about react and next.js I'm going to have it linked down in the description for you but this course covers literally everything you need from an absolute beginner all the way to Super Advanced react nextgs features so I highly recommend you check it out

Original Description

React Simplified Course: https://reactsimplified.com/?utm_source=youtube&utm_medium=video-description&utm_campaign=video-id-_tUdtt6H5CE React recently announced a new experimental hook that makes working with forms so much easier. This hook solves issues related to double submitting forms, loading states, and so much more. 📚 Materials/References: React Simplified Course: https://reactsimplified.com/?utm_source=youtube&utm_medium=video-description&utm_campaign=video-id-_tUdtt6H5CE 🌎 Find Me Here: My Blog: https://blog.webdevsimplified.com My Courses: https://courses.webdevsimplified.com Patreon: https://www.patreon.com/WebDevSimplified Twitter: https://twitter.com/DevSimplified Discord: https://discord.gg/7StTjnR GitHub: https://github.com/WebDevSimplified CodePen: https://codepen.io/WebDevSimplified ⏱️ Timestamps: 00:00 - Introduction 00:57 - Traditional Solution 02:45 - useFormStatus Solution #ReactJS #WDS #useFormStatus
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Web Dev Simplified · Web Dev Simplified · 0 of 60

← Previous Next →
1 Introduction to Web Development || Setup || Part 1
Introduction to Web Development || Setup || Part 1
Web Dev Simplified
2 Introduction to Web Development || Understanding the Web || Part 2
Introduction to Web Development || Understanding the Web || Part 2
Web Dev Simplified
3 Introduction to HTML || Your First Web Page || Part 1
Introduction to HTML || Your First Web Page || Part 1
Web Dev Simplified
4 Introduction to HTML || Basic HTML Elements || Part 2
Introduction to HTML || Basic HTML Elements || Part 2
Web Dev Simplified
5 Introduction to HTML || Advanced HTML Elements || Part 3
Introduction to HTML || Advanced HTML Elements || Part 3
Web Dev Simplified
6 Introduction to HTML || Links and Inputs || Part 4
Introduction to HTML || Links and Inputs || Part 4
Web Dev Simplified
7 Learn Git in 20 Minutes
Learn Git in 20 Minutes
Web Dev Simplified
8 5 Must Know Sites For Web Developers
5 Must Know Sites For Web Developers
Web Dev Simplified
9 10 Best Visual Studio Code Extensions
10 Best Visual Studio Code Extensions
Web Dev Simplified
10 Learn CSS in 20 Minutes
Learn CSS in 20 Minutes
Web Dev Simplified
11 How to Style a Modern Website (Part One)
How to Style a Modern Website (Part One)
Web Dev Simplified
12 How to Style a Modern Website (Part Two)
How to Style a Modern Website (Part Two)
Web Dev Simplified
13 3D Flip Button Tutorial
3D Flip Button Tutorial
Web Dev Simplified
14 How to Style a Modern Website (Part Three)
How to Style a Modern Website (Part Three)
Web Dev Simplified
15 Animated Loading Spinner Tutorial
Animated Loading Spinner Tutorial
Web Dev Simplified
16 How to Write the Perfect Developer Resume
How to Write the Perfect Developer Resume
Web Dev Simplified
17 Animated Text Reveal Tutorial
Animated Text Reveal Tutorial
Web Dev Simplified
18 Learn Flexbox in 15 Minutes
Learn Flexbox in 15 Minutes
Web Dev Simplified
19 Custom Checkbox Tutorial
Custom Checkbox Tutorial
Web Dev Simplified
20 Start Contributing to Open Source (Hacktoberfest)
Start Contributing to Open Source (Hacktoberfest)
Web Dev Simplified
21 JavaScript Shopping Cart Tutorial for Beginners
JavaScript Shopping Cart Tutorial for Beginners
Web Dev Simplified
22 Responsive Video Background Tutorial
Responsive Video Background Tutorial
Web Dev Simplified
23 1,000 Subscriber Giveaway
1,000 Subscriber Giveaway
Web Dev Simplified
24 How To Prevent The Most Common Cross Site Scripting Attack
How To Prevent The Most Common Cross Site Scripting Attack
Web Dev Simplified
25 Transparent Login Form Tutorial
Transparent Login Form Tutorial
Web Dev Simplified
26 The Forgotten CSS Position
The Forgotten CSS Position
Web Dev Simplified
27 How to Code a Card Matching Game
How to Code a Card Matching Game
Web Dev Simplified
28 10 Must Install Visual Studio Code Extensions
10 Must Install Visual Studio Code Extensions
Web Dev Simplified
29 Learn CSS Grid in 20 Minutes
Learn CSS Grid in 20 Minutes
Web Dev Simplified
30 Learn JSON in 10 Minutes
Learn JSON in 10 Minutes
Web Dev Simplified
31 10 Essential Keyboard Shortcuts For Programmers
10 Essential Keyboard Shortcuts For Programmers
Web Dev Simplified
32 What Is The Fastest Way To Load JavaScript
What Is The Fastest Way To Load JavaScript
Web Dev Simplified
33 Differences Between Var, Let, and Const
Differences Between Var, Let, and Const
Web Dev Simplified
34 How To Install MySQL (Server and Workbench)
How To Install MySQL (Server and Workbench)
Web Dev Simplified
35 Learn SQL In 60 Minutes
Learn SQL In 60 Minutes
Web Dev Simplified
36 How To Solve SQL Problems
How To Solve SQL Problems
Web Dev Simplified
37 What Are Design Patterns?
What Are Design Patterns?
Web Dev Simplified
38 Null Object Pattern - Design Patterns
Null Object Pattern - Design Patterns
Web Dev Simplified
39 Your First Node.js Web Server
Your First Node.js Web Server
Web Dev Simplified
40 How To Setup Payments With Node.js And Stripe
How To Setup Payments With Node.js And Stripe
Web Dev Simplified
41 How To Learn Any New Programming Skill Fast
How To Learn Any New Programming Skill Fast
Web Dev Simplified
42 Asynchronous Vs Synchronous Programming
Asynchronous Vs Synchronous Programming
Web Dev Simplified
43 JavaScript ES6 Arrow Functions Tutorial
JavaScript ES6 Arrow Functions Tutorial
Web Dev Simplified
44 Are You Too Old To Learn Programming?
Are You Too Old To Learn Programming?
Web Dev Simplified
45 JavaScript Cookies vs Local Storage vs Session Storage
JavaScript Cookies vs Local Storage vs Session Storage
Web Dev Simplified
46 JavaScript Promises In 10 Minutes
JavaScript Promises In 10 Minutes
Web Dev Simplified
47 Builder Pattern - Design Patterns
Builder Pattern - Design Patterns
Web Dev Simplified
48 JavaScript == VS ===
JavaScript == VS ===
Web Dev Simplified
49 JavaScript ES6 Modules
JavaScript ES6 Modules
Web Dev Simplified
50 8 Must Know JavaScript Array Methods
8 Must Know JavaScript Array Methods
Web Dev Simplified
51 CSS Variables Tutorial
CSS Variables Tutorial
Web Dev Simplified
52 JavaScript Async Await
JavaScript Async Await
Web Dev Simplified
53 How To Choose Your First Programming Language
How To Choose Your First Programming Language
Web Dev Simplified
54 Easiest Way To Work With Web Fonts
Easiest Way To Work With Web Fonts
Web Dev Simplified
55 Singleton Pattern - Design Patterns
Singleton Pattern - Design Patterns
Web Dev Simplified
56 Responsive Navbar Tutorial
Responsive Navbar Tutorial
Web Dev Simplified
57 CSS Progress Bar Tutorial
CSS Progress Bar Tutorial
Web Dev Simplified
58 Learn GraphQL In 40 Minutes
Learn GraphQL In 40 Minutes
Web Dev Simplified
59 What is an API?
What is an API?
Web Dev Simplified
60 Learn How To Build A Website In 1 Hour!
Learn How To Build A Website In 1 Hour!
Web Dev Simplified

This video teaches how to use React's experimental useFormStatus hook to simplify form handling and improve user experience. It covers the benefits and usage of this hook, including creating loading states and managing form submissions. By the end of this video, you will be able to use the useFormStatus hook to improve your React applications.

Key Takeaways
  1. Create a new React project and install the required dependencies
  2. Import the useFormStatus hook from React DOM
  3. Create a custom component within a form to consume the form context
  4. Use the useFormStatus hook inside the custom component
  5. Pass a function to the action function to call the original onSubmit function and cancel the event
  6. Use the form data object to access form input values without a ref
💡 The useFormStatus hook simplifies form handling by automatically setting the loading state to true when creating a new form submission, reducing double form submissions and improving user experience.

Related AI Lessons

Chapters (3)

Introduction
0:57 Traditional Solution
2:45 useFormStatus Solution
Up next
The Sales Formula That Changed Everything
The Payrollin' Podcast
Watch →