Complete React Tutorial (& Redux) #9 - Intro to Forms

Net Ninja · Beginner ·🌐 Frontend Engineering ·7y ago
Skills: React90%

Key Takeaways

Covers forms in React and capturing user data

Full Transcript

all right they're my friends so a big part of most websites is user input in some way shape or form whether that's on a contact form or updating a profile page or writing on a message board and we need a way to work with forms and the data that a user inputs within react so I'm gonna show you how we can do that or rather introduce you to how we can do that so you can see already what I've done is stripped back our component in templates so now all I'm doing is I'm storing these on the state still and I'm just outputting h1 here to say my name is this dot state dot name and we can see that very simply in the browser right there so all this is very simple at the minute now what I'd like to do is create a simple form just a single input field where a user can type in their name that's going to update the state on the fly and update obviously therefore where we output it as they type in and then when they click on submit it's going to log their name to the console all right so then let's get started with this form underneath the h1 over here we'll create a form tag first of all and close that off now inside the form tag and make sure you spell that correctly we want just a single input field so let's say import the type is going to be equal to text so type text and then we'll just close it off like so all right now underneath we need a button and this is going to be a submit button like so all right so that's how a simple HTML or JSX for the form very simple like I said we're going to type a name in here and then we're going to submit it and as we type in here I want it to update on the fly over here all right so then how do we do that how do we detect when a user is typing inside this input field and then change the state the name over here based on what they type well pretty simply we use an event so we say on then the event name for this is change and this fires every time there's a change of value inside this input field so every time a user types a new letter or deletes a letter etc so we want to equal that to a function that's gonna fire every time there is a change but first of all we need to define that function so let's call this handle change and remember we're going to use the state inside here so we need an arrow function so we can get the value of this inside the function so remember to change the state we say this dot set States because we're going to change the state every time a user changes the input field right so this dot set state and inside we want to change the name property but how do we get the value of whatever the user has typed in here because that's what we want to change it to pretty simply we have access to the event object so we can say E and we can grab the target element which is the input for your by saying e dot target now on this target we have a property called value and that is going to get the value of whatever is inside this input field at the moment that this function fires this handle change function so every time a user types it's gonna fire this it's gonna set the state and change it to the value of whatever is currently inside this input field does that make sense all right so let's give this a quick whirl will reference the function down here this dots handle change like so save it and go over to the browser and I'm going to change this to Yoshi and you can see as we type over here is updating on the fly in the browser over here where we output it as well so that's cool that's all working that's how we keep track of what user is typing into an input field all right so now the next thing we want to do is react when the form is submitted either when a user clicks enter or click Submit because at the minute the default behavior is just to refresh the page we don't want that we want to take the value of the state the name in the state and log it to the console so first of all we need to go over here to the form and add on an odd submit event handle all right now you might have thought that we do an on click here now that would work for when your user clicks on the button but not when they click enter because an end also submits the form so by doing on submit instead up here we're capturing the enter if a user clicks answer to submit or whenever a user clicks on a button inside the form as well so that captures both of those all right so we need to define the function which is going to be called when a user submits this form so let's come under here and say handle submit so that equal to an arrow function take the event object and inside here we want to first of all prevent the default action of a form being submitted now remember when we submit a form the default action is for the page to refresh I can type a lot of stuff in there submit the page refreshes we want to prevent that default event behavior now the way we do that in JavaScript and this is just normal JavaScript is by saying e dot prevent default and now that is going to prevent the default behavior of the event being submitted ie to prevent the page from being refreshed so first of all we need to reference this handle submit over here so we can say this don't handle submit so reference that so now when we submit the form over here we can type in a lot of stuff Yoshii and then press submit and nothing is log to the console but at least we prevented the page from being refreshed now all right cool so the next thing we want to do is log out the name to the console so let's say console log and we'll say farm submit it like so and then we'll also log out this dot state dot name so if we save that now and come over here to the browser I'm going to type in Mario and then submit and we can see farm submitted Mario cool so there we go that is your introduction to farms we are going to be working with farms in much more depth as we go through the rest of the course I wanted to give you just a little primer now so that when we come across them later on it's not brand new to you all right so I think a lot of the basics of react have been now and we want to take it up a notch so in the next tutorial I'm going to show you how we can use create react app to create a full application so that we don't have to have our components in a script tag down here but we have a full react settle for a full web application so I'm going to show you how we do that in the next video

Original Description

Hey gang, in this React tutorial I'll introduce you to the concept of forms in React and how to capture user data and store it on the component state. ---------------------------------------- 🐱‍💻 Course Links: + VS Code editor - https://code.visualstudio.com/ + GitHub repository (course files) - https://github.com/iamshaunjp/react-redux-complete-playlist/tree/lesson-9 + React CDN - https://reactjs.org/docs/cdn-links.html ---------------------------------------- 🤑 Donate @ https://www.paypal.me/thenetninja 🎓Find me on Udemy @ https://www.udemy.com/user/47fd83f6-5e4a-4e87-a0f0-519ac51f91b6/
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 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related AI Lessons

Up next
The masks we wear | Zora Krstić | TEDxLuxembourgCity
TEDx Talks
Watch →