4.1 - Friend List App Component
This app will be highly useful in illustrating how to pass data from a form using React. Chances are, you'll have to do it. You'll notice some patterns from previous applications, just different syntax.
imports and API_Base
Set up a few imports and a class component. See the code below. We encourage you to type everything except for the API_BASE.
Just like in plain JavaScript files, we're keeping our API_BASE capitalized because it is convention to put caps on constants, and this is a base that will be a constant.
Friend List App Form
Let's create a Friend List App form now inside of the render and inside of our two container divs. Note that we put some extra space for clarity:
Add the routes
We need to get our routes set up.
Just so you can practice and get stronger, we'll leave it up to you to go to
Sidebar.js
and do this. We are calling our route 'friendlist'. Don't forget that examples of adding a route are available in Part 1, section 1.4.When you fire up the app, and go to that route, you should get a form with two input fields and a button. Again, you should see something like this form in this app at this point (styling may be different, you can always style later):
Of course, if you press submit, there is no data being passed. We are going to need a method for that.
Constructor and friends array
We are going to need an array for our collection of friends. Just like in the past, we are going to set the state of the array in our constructor, before anything renders:
We pass in props, as we've discussed in the past, and we set the initial state to having a blank array called friends
. Remember that you can console log the state by console.log(this.state.friends)
right before the closing curly brace. You should see the empty array in Chrome:
Add a handleSubmit method
A good way to wire up a button with a method is to first simply log something when you press the button. We'll now add a handleSubmit()
method. Eventually, this will take care of our http post. First, we need to set up some variables inside the method:
It's important to note that handleSubmit is not a React method. It's not a life cycle method or any other built in method in the React API.
It's just a convention that is used by React developers to call something that is handling some event with the word 'handle' as the starting word in the method signature.
Call onSubmit()
Nothing is happening yet though. We need to connect the button to the method. Let's call this function in the form. We can call it in the the onSubmit of the form:
Now, when we press the button, our console.log in the handleSubmit
method should fire off. You should see this:
Fetch
So we now have the button hooked up and we need to pass that data off to our API. To do that we'll use fetch to make a post request to the api url. For clarity, we went ahead and tore out the console.log
methods in the handleSubmit
method. You can keep them, if you'd like. Under the newly created variables let's add the following post request:
You should get the response data back in the console. Woohoo!
Notice also that the form clears when we hit submit. That's because we used refs
again, where we set this.refs.name.value=""
. As we get the response from the server, we set the input fields to blank strings. This is handy to know!
The Final Code
In the next module we'll create the actual Friend
component. Here is the final code for this section:
Last updated