From the course: React Practice for Beginners: Build and Modify Basic Components
Unlock this course with a free trial
Join today to access over 24,700 courses taught by industry experts.
Submitting a simple form - React.js Tutorial
From the course: React Practice for Beginners: Build and Modify Basic Components
Submitting a simple form
- [Presenter] Let's submit a simple form. Now, that our inputs are connected to the state, we often want to do something when the user submits the form. For example, we often want to save the data and show a message. We will handle this using an event handler, usually one on a button click or a form submission. Let's go ahead and modify our code. So what I'm going to do, I want to go ahead and show all these submitted names below the form. So let's go ahead and create a state for that as well. I'm going to initialize this state to an empty list. All right, good. Let's go ahead and add a handle submit function. So I'm going to say constant handle submits and could specify error function in here. What I'm doing in here is sometimes a little bit difficult, but first, I'm checking if name is set and if that is the case, I'm going to set the submitted names and what I'm doing in here, I'm using the spread operator. And what this does, it's a long list to be expanded into its separate…