1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Create a React Form in Gatsby

    Jason LengstorfJason Lengstorf
    gatsbyGatsby
    >=2.15.7

    Each video chat will happen in a "room". To do that, we need a way for people to identify themselves and choose which room they want to join.

    In this lesson, we'll create a form with useState React hooks that accepts user input.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: We need to allow people to join a room. To do that, we need a form. We're going to create a component and that will be called join.js. Inside, we're going to import React and we also need to track state of the components, the input specifically. We're going to import use state from React.

    Our component is going to be called join and it doesn't take any props. It will however keep track of the identity of the person, that'll be their first name or whatever they want to go by. A set identity, which is how we're going to set that and that will come from use state, which will start as an empty string.

    Then, we need to do the same thing for the room name. We'll set up room name and set room name from use state as an empty string as well. The form itself is going to be inside of a fragment because we want to send back an H1 that'll say, "Start or join a video call." Then, we need to have our form as well.

    To use the CSS, that is predefined in layout CSS over here, we're going to use class names, so this is going to be called start form. Inside of it, we need to have three things. We need to get the name, the room name and a button to submit the form.

    Let's start by adding a label and we're going to make this accessible. We'll add an HTML 4, that will be for the identity. Inside of this, we'll use display name and then we need to put in an input.

    The input is going to be a text type. It will also have an ID of identity that needs to match the HTML 4. These two need to be the same. The value is going to be this identity variable here. We'll pull that in and on change, we need to update that.

    We will get the event and we will use the set identity helper from use state to update the value to the event target value. That'll be whatever somebody types into this input gets saved using set identity into this identity variable.

    We need to do exactly the same thing for the room name, so we can duplicate this block entirely. We'll change this to room name and then we need to update this one to use the set room name helper instead.

    Finally, we need to set button of type submit and will say, "Join video chat." Next, we need to actually put this on our home page because if we look at our page right now, nothing's happening.

    To use it, we're going to go into index.js. We're going to import join from components join and then we're going to swap out this H1 for our join component. Now that we've defined this component, we need to export it as our default. We'll export default join.

    Once we've saved this, we can go back out to our preview. Because gatsby develop is still running, it'll auto update it. We can jump over here, reload the page, and we'll see our component. It shows our headline and our form. If we update it, we can see that the values have updated.

    However, nothing happens yet. If I submit this, we don't see anything happen. It just refreshes the page. Next, we need to add an actual submission handler.