5.1 - Form Component
In the last section, we set up our movie app, but now we need to create our next component Form
. This component will be where we accept user input, query the API based on that user input, and then send the results to another component to display.
Let's just get started by setting up our component and make sure that it's displaying in our app. First, go into Form.js
. Then type the following code into it:
Now you should see "This is a form" in your app.
state
Next thing we need to do is think about our state structure. What kind of state do we need, what information do we need to keep track of? All we're wanting to do here is have the user type into an input field and get results based on that input. It makes sense to have the results in the state, but really that's about it. Let's go ahead and set up our constructor in our Form
. Notice how we're setting up the state to hold the results here. Go ahead and type the following in your Form
component.
render()
So now that we've got our state set up, we can think about what we're going to need to GET those results. We need an input for the user to type in and then some actions to occur when that happens to go get information from an API. Let's think about what we need to render to the screen. You can take your h1
out of the render and instead put in a form to capture their input. All that really needs to be in this form, is an input. This input should do something when they type, we can use onKeyUp
for this.
Additionally, if they try and submit the form somehow, we need to make sure we're preventing the default action of the form, just like always. Go ahead and type the following code in your render.
Notice that we've called our two methods handleSubmit
and handleKeyUp
. It's convention to use handleSubmit
for form submission and handle in general when dealing with user input. Now that we have these methods, we need to define them!
handleKeyUp
Now that our render can use these methods, we need to actually make them do things. Let's first start with handleKeyUp
. When someone types or "keys up", we want it to hit our api. We want to be able to give them suggestions based on what they're typing in. So, for example, if they can't quite remember the movie name, but know it starts with "hidden", then they'll see a list of responses based on "hidden". Let's go ahead and create our method, and comment out some of the things that we want to actually happen in this method.
Make sure you understand what we're trying to do here. Every time a user types anything in, we're going to hit the api and see what movies match what they've typed. Then, we're going to store the results in our state. We also need to handle errors so they don't break our application.
Pointing to the API
The first step is to hit our API, and pass in useful info such as the value the user typed in. For this project we are using The Movie DB API, so before we can even set up how to get there, we need to go check out the docs. As with most open APIs, there are rate limits on this API, so you always want to have a unique key for your application, that no one else can use, so you can use all of your own API requests within your limit.
Steps to Getting an API Key
Email verify and sign in to your account.
Click on your settings on your account.
Click the API button on the menu
Click Create
Click "Developer"
Accept the terms of use at the bottom.
Call your application anything you want. You can use it to indicate that you're learning. Similarly, for application URL you can put localhost for now. Fill out the rest of the information. You can use pseudo-information, if you'd like.
Now you should see your key. You can grab the
v3
auth version.
Now that we have our API key, we're ready to move on to hitting the API.
The first thing we need to do is set our API key up in a const
variable. We use const because this won't change.
then()
Next, just like ajax calls, we can do things with the results of our query, and we need to, to take the results and put them in the state. So, in our then
we can grab the results and set our state with them.
Error Handling
So, now that it works on successful responses, we also need to handle errors. We shouldn't try and set state if the API doesn't respond correctly. Our whole handleKeyUp()
should look like this. We're console logging our errors so that we can see and fix them.
preventDefault()
Now that we've set up our handle key up to handle all the API requests, we just need to prevent the default action of the form submit from happening. We don't need anything in particular to happen there, so we want to preventDefault, just like we have previously. Type this method out just below your constructor and above handleKeyUp()
.
Form.js
Your complete Form
component should look like the following block of code.
In our next part, we'll create our FormResults
component.
Last updated