Create a React Form in Gatsby

Jason Lengstorf
InstructorJason Lengstorf
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 4 years ago

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.

Instructor: [00:01] 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.

[00:18] 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.

[00:36] 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.

[00:57] 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.

[01:13] 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.

[01:25] 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.

[01:44] 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.

[02:01] 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.

[02:13] 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.

[02:26] 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.

[02:44] 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.

[03:01] 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.

egghead
egghead

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today