Exercise 04 : Load and parse JSON and show loaded movies data
Introduction
This exercise teaches how to load JSON data and show loaded data in a React application. This movie example application will load data from the movie db.
The Movie DB
Note
Don't create any aws accounts/projects, just get your API key from your settings!
Sign up to The Movie DB and get the api key. Learn the basics of API and Getting Started.
You can find your API key from your profile/settings. An example request looks like this (replacing the api_key_here
text with your own API key):
1 |
|
Example response will be returned with JSON data:
For example, Now playing movies will be used in this tutorial with a following API call:
1 |
|
React project
Create a new React project and name it to movies-app
.
1 |
|
Install and use Axios
Install Axios and use it to load JSON data in your application
1 |
|
MovieList component
Create a new MovieList
component inside your App.js
. This component will load and hold all the movies data. Now it only render p
element with MovieList
text.
1 2 3 4 5 |
|
And remember use it in App.js
.
1 2 3 4 5 6 7 |
|
Use React hooks to store loaded movies data in your MovieList
component.
1 |
|
Load movies data with Axios and React effect hooks.
1 2 3 4 5 6 7 |
|
Note
Remember use your own API key in above request.
MovieListItem component
Create a new MovieListItem
component inside your App.js
. This component will render one movie data in the UI. Now it will only display movie title text.
1 2 3 4 5 |
|
Modify MovieList component
Use MovieListItem
component in MovieList
component rendering. A map
function loops through all the movies. Movie object will be send to MovieListItem
component via props.
Now conditional rendering is done with movies array length. Loading, please wait...
is displayed, when data is loading (if there aren't any movies inside a movies
array). You should implement timeout to display for example Can't load movies
text if data is not available.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Modify MovieListItem component
Modify MovieListItem
component to display more data from the Movie. Check the Movie DB API, which data is available. Be innovative!
Show more information
Now you are only using now_playing
flag in request and it doesn't get movie detail information. You can query a more movie specfic data from the Movie DB with a below command
1 |
|
Note
You need to specify correct movie_id
and your own api_key_here
.
Modify MovieListItem
component to use Axios and load movie specific data with React Hooks effect.
1 2 3 4 5 6 7 8 9 |
|
You can display for example Movie poster, genres and video based data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
And use return to render some movie based data:
1 2 3 4 5 6 7 8 9 |
|
You can create own styles or use below ones in App.css
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
Try to use react-modal and react-youtube npm packages. Open a modal and show a selected movie video. You might need to pass selected video id to parent component via props (it depends which component is opening a modal window).
Push exercise 04 / Movies to GitLab
Test your application in web browser, take screenshots and commit/push your project and screenshots back to JAMKIT/GitLab. Remember move your exercise/issue ticket from Doing to In Review in Issues Board and write your learning comments to issue comments.
Save screenshots to movies-app folder.