3.1 - NytApp Component
Planning and Thinking about What We are Building
So, we know what we are trying to build, as we've already built it in vanilla JS. We need to build a page that fetches information from the NYT API when a user submits a form. Then we're going to display that information nicely to the screen. In Vanilla JS we needed to create an HTML file and a JS file. In React we can, of course, just do JSX. We used about 190 lines of code in our vanilla JS, and here we'll do around 100, not that lines of code is super important. However, our React code should be much more readable and easy to refactor. Since we know what we'll need to build, we can think about what kind of components we'll want. We'll definitely need our parent component (NytApp) to handle the data, and then we should probably make a separate component to display information. You can always break them down more if it makes sense to you.
Setting up our Component
Since this is our parent component for this mini-application, we'll be handling the storing of information here, hence why we'll need to use state. For now, we can set it up as an empty state while we think about what we'll need to store. When looking at the vanilla JS version of this app, we see that we created a LOT of variables at the beginning. For our state, we'll be using some of the same ones to keep information, but we don't need as many as we needed in vanilla JS.
Setting Up the State
Thinking about state first is a good way to organization your application. When we think about what data we need to store, use, and change, we can think about what we want to put in our state to allow us to do that. For our NYT app, we need to store the information from the user, the search term, the start date and the end date, so we can send these to our API, so we'll need to create those in the state. We also want to capture the results from our API, so we can call that 'results', and we want to keep track of our page number. Let's fill out our state like below.
That should take care of all the data we need to store that can change depending on how the user interacts with our application. We also have a couple of other things we need to store in variables and use, but these things will not change as the user interacts with our application, so we can declare them as const
s.
API URL & Key
Since our base API url and the API key won't change as the users interact with the application we can declare them as const
s outside of our class component. Make sure you use the same API key you created before for the NYT application. Below your import statement and above your export class ...
statement, declare the following const
s.
Making API Calls
So, now that we have our baseURL and API key set up, we can start to set up our fetch. We're going to create a method that will fetch results for us, and we can call this when we need to (when the user submits the form, when the user presses next or previous page, etc.). Underneath and outside of the constructor, but before the render, write the following method.
Let's break down what we are doing in fetchResults
.
1) First thing, is notice that we are using an arrow function here. In react, this means that we do not have bind this method, because arrow functions do not create their own this, therefore they use their parent's this. If we hadn't used an arrow function here, we would need to bind this function to this in the constructor, in order to be able to use this.setState
or this.state
.
2) Second, we are creating our url variable based on the baseURL
, the key
, and also the pageNumber
and search
which we stored in the state. We'll worry about how to update those variables based on inputs later, for now we know we want to send whatever is in the state in our GET request.
Creating a Form
Cool so now we have set up our fetchResults
, we need to actually be able to change the information in our state when our user interacts with our application. So far we have nothing in our render for our user to be able to interact with, so let's work on that now. Just like in vanilla JS, we can use a form to capture input from our user. Let's make our render function look like this:
To make this form, we actually grabbed the form from the vanilla JS code, and just condensed it down a bit. We also added a couple of things that are specific to react, specifically the onSubmit
and onChange
. These are named pretty accurately for what they do. onSubmit
does whatever you tell it to on submit of the form, and onChange
does whatever you tell it to when a change happens in the input. We haven't done anything in these yet, so that'll be our next step.
Creating Methods for Submit and Change
So we need two methods to create, one for when the user submits the form, and one for when they type something into the inputs. Let's start with the form submit method. Let's put this function directly below and outside of the constructor.
Let's break this method down. We're taking in an event, and then calling the fetchResults
function. We're also calling event.preventDefault()
which if you remember from vanilla JS, just prevents the default action, which in this case is the page refreshing, which we definitely do not want in react. That's really it. Next, let's look at how we can take the input from a user, and use it to set the state correspondingly. Type this method below your handleSubmit
and above the fetchResults
method.
Updating our Render
Now, we need to update our render to actually use the methods we created. Make your render look like the following:
Before moving to the next step, check your code against the code below, it should look like this:
Last updated