Connecting to a server
It's time to make our app a little more interesting by hooking it up to a server! 🤓
Last updated
It's time to make our app a little more interesting by hooking it up to a server! 🤓
Last updated
Hopefully, you now have created a Movie
component, and rendered multiple of them in the MovieList
component. In order to make the application more dynamic, we need to connect to a API.
Please take some minutes to take a look a possible solution at . Here, the components now live in their own files, making reusability easier.
But first. In the next assignment, you will get acquainted with the . They offer good solutions for a wider range of usages - like calling APIs. Here is the precode for this assignment:
Allright, this may seem like a lot - but we will explain it all in nice, bite-sized portions.
So, the first part is:
...the first component mount (the first time the component is injected into the DOM)
...every render
We can also use lifecycle methods to decide when the component should re-render. You can think of re-rendering as updating the HTML at our website. In this assignment, we are using one of the lifecycle methods - componentDidMount
:
Here, we define what we want to happen right before rendering our component. And, in this case, this means calling the API to get our data!
In the beginning of our class component, we have defined a component state. This is the data that only concerns the component itself, and is very powerful in React app development! In the state, we define the variables that can change inside the component throughout its lifecycle. This is so that we can render different things, or render things differently, based on the state (props mentioned in the previous section can also be used for this).
In a React class component, you actually only really need to implement one function - the render
function:
The render
should return what you want to render to the website from the Component in question.
In this example, we render differently based on the component state. If the state's pending
variable is sat, we only render a loading-screen (kindof). Elsewise, we return our full app containing the MovieList
!
So, in order to change the state, we need to set the state, which is what happens in componentDidMount
:
This is the React way to change the component's state. Finally, inside componentDidMount
, the API is called, and the state is set accordingly.
Allright, so after adding the API call, the movies
array is now populated from the server. The array consists of object, looking like the following example:
As we already have taken care of showing the title
of the movies from previous assignments, we are already rendering that! Now:
Choose 2-3 other variables that you want to show in the Movie
component (suggestions:release_date
, vote_average
and vote_count
).
Also, add an img
inside the Movie
component, with the src
attribute sat to the poster_path
given from the API.
This is how you define a class component in React. This makes it possible to add , for example methods that should be run on e.g.:
...unmounting the component (essentially this means when leaving the component, and detaching it from the )
This assignment precode is found at , and the tasks are found at the bottom of this page.