React Context & Hooks Tutorial #11 - useState with Forms
Key Takeaways
Demonstrates using the useState hook with forms in React
Full Transcript
okay then so now we've seen this use state hook in action now what I'd like to do is take that and apply it to some other kind of scenario for example working with forms so typically when a user type something into an input field in react what we like to do is keep track of what that user types in and put it into some kind of states property so we're going to do that now we're going to give the user a form to add a new song where they type in the title of the song and we're going to store that in a local state using this hook use state now to do it we're going to create a brand new component so let's call this new song form Jas and inside that we still need to import react we also want to import use state because we will need that hook and then I'm gonna do a stateless functional components tab we'll call this new song form and then inside here we want to return some kind of template so it's just gonna be a very simple formulas so we'll do our form tags then we'll do a label tag and this will say something like song name and then underneath that we need an input field that the user can type that song into so input the type is going to be equal to text and then we're also gonna make this required like so okay now finally we need a submit button so input type is equal to submit and the value is going to be equal to add song okay so now we have this little form right here what I'd like to do is nest this component inside the song list component at the bottom instead of this button so let us now say new song form press enter it's going to auto import that at the top for me and leave it at that save it let's go over to the browser and we can see this form right here now at the minute nothing's going to happen right but what we want to do now is attach some kind of event listener to this form right here so I'll say on submit and then set it equal to something and we'll define this function later on but we also want to track what our user types into this thing so we can do that in react by saying on change so whenever the value inside this change is ie whenever they start typing something in after each keystroke then this is going to fire a function right here as well now this function is just going to be an inline function that I'm going to write here an arrow function and we taking the event object now ultimately what we want to do inside this function right here is store what I user types in into some kind of local state now to use that local state we're going to use this hook use States so use States and we pass in an initial value for a bit of state that we want to use in our case it's going to be just an empty string then as a user types into this on every change after each keystroke then we want to update the state with whatever the user has typed in so remember we get back an array from this with two values the actual value which we'll call title and a function to update that which we'll call set title so we'll say Const and then square brackets is equal to use state the first one is called title the actual value then set title for the function to change that value so what we want to do inside this function right here is actually use set title to update this piece of state with whatever the user typed in at that moment in time so we're doing this on one line so we don't need those curly braces instead we'll just say set title and we want to pass in the value that's currently inside this input form so we'll use the event object then we'll say dot target to get this input field then dots value so that's going to get us the value of whatever is inside this input field at that moment in time and it's going to apply it to this thing this piece of state okay so we're now keeping this in synch with whatever is inside this input field now I'm also going to attach over here a Val profit or a value property rather and set it equal to title like so so we're setting it equal to whatever is in the state so to begin with when we first log the page that's obviously going to be nothing okay so now if I save this actually nothing yet is gonna happen because when we change this all we're doing is updating the state and we're not login anything out or anything like that so in fact let's create now a function to handle the submit so we'll call this handle submit and then up here we'll define that function so Const handle submit and set it equal to an error function we taking the event object and inside here the first thing I'd like to do is say e dot prevent default just to prevent that default action of the form when we submit a form and that is to refresh the page we don't want that to happen so we're stopping that right here and then what we'll do is just log out the title so whatever a user has typed into this input field so we'll say console dot log title okay so let me save this now and preview it in the browser over here so now as we type something in if we go to the console over here and press add song now we can see this is in the console so now it's working but what we need to do is take this and actually output it so we want to add it to this data in the song list over here so to do that to change this data we need a function which is going to take what a user types in here and then add it to this data so we already have a function here that we defined in the last lecture called add song and that takes all of the current songs have puts them in a new array plus a new one now it had codes the title but what we want to do now is output the title that we type in here so we can take that in as a parameter here we'll call it title and then we'll set title equal to title now I can use a bit of es6 shorthand for this because the property name and the value of the variable is the same so I can just say title like so and that does the same thing so we now need to pass this add song function into this new song form component so that we can call this function from this form component or peer when we submit the form and when we do that we pass in the title so it receives that title and then it adds it to the songs array so let's do that let's come down here and we'll say at song is equal to add song so we're just using a standard prop right here save that and then we need to accept it up here inside an object so that's song like so and then over here where we're currently logging it to the console instead we'll say add song and we want to pass in the title which is this bit of data which is kept in sync with what a user types into the field so let's save this and see if it works head over to the browser and I'll just say test press ENTER and it works and I can do something else I've sung it works cool now one thing I'd like to clear this field when a users type something in and pressed add song because at the minute if we want to add another one we'd have to delete that first then add it in so I want this to auto clear dead simple to do or we're gonna say is set title which is the function we can use to update this over here set title and we're going to change that back to an empty string and because this value of this input field is kept in sync with this piece of state when we do that and we update this piece of state then this input field is going to update as well with that empty string so let's save that and preview so testing yep it works one two three it works awesome so that's one nice use of use state to use it with input fields inside a component
Original Description
Hey gang, in this React hooks tutorial we'll look at the useState hook in conjunction with text input form fields to track what a user types into them.
----------------------------------------
🐱💻 🐱💻 Course Links:
+ Course files - https://github.com/iamshaunjp/react-context-hooks
+ Complete React Course - https://www.youtube.com/playlist?list=PL4cUxeGkcC9ij8CfkAY2RAGb-tmkNwQHG
+ React Context Docs - https://reactjs.org/docs/context.html
+ Modern JavaScript Tutorial - https://www.udemy.com/modern-javascript-from-novice-to-ninja/?couponCode=NINJAYT
🐱💻 🐱💻 Other Related Courses:
+ Firebase Firestore Playlist - https://www.youtube.com/watch?v=4d-gIPGzmK4&list=PL4cUxeGkcC9itfjle0ji1xOZ2cjRGY_WB
Firebase Authentication Tutorial - https://www.youtube.com/watch?v=aN1LnNq4z54&list=PL4cUxeGkcC9jUPIes_B8vRjn1_GaplOPQ
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
Related AI Lessons
⚡
⚡
⚡
⚡
Next.js vs Remix vs SvelteKit: Which Framework Should You Learn?
Dev.to · Etrit Neziri
Had my Frontend Developer interview with Capgemini (Application Developer) today, and I wanted to…
Medium · JavaScript
10 Frontend Developer Tools to Boost Productivity in 2026
Medium · Programming
10 Frontend Developer Tools to Boost Productivity in 2026
Medium · JavaScript
🎓
Tutor Explanation
DeepCamp AI