Exercise 04 : Movies
Purpose and learning process
In this exercise you will learn the basics of the React Native CLI development:
- using and rendering build-in components
- creating and rendering an own components
- understand state and props
- event handling
- passing data between components
- calling functions between components
- using styles
- navigating between screens
- load and parse JSON data
- touchable components
This tutorial teaches you to create React Native application with React Native CLI. App will load it’s data from the Movie DB. Application UI will be created with build-in React Native components and styles. Created project is tested with a emulator and a real device. To test with emulators you will need to install Android Studio or Xcode.
Example screenshots
Application will load data from The Movie DB and display movie based data with own components.
Example Video
Example video: https://youtu.be/tnKkw_Emv9s.
The Mobie DB
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 |
|
Note
Add &append_to_response=videos
to your request, if you want to include trailers to response data
Example response will be returned with JSON data
Create a new project
Create a Movies App with React Native CLI. You can find setup instructions for Android and iOS from Setting up the development environment. Instructions are a bit different depending on your development operating system, follow instructions and set up your system ready for React Native CLI.
When your system is ready for React Native CLI development, type a following command to create a Movies project:
1 |
|
After that you can run your app for iOS or Android:
1 2 3 4 5 6 7 8 9 10 |
|
Run your project
For example running in Android, first start Android emulator and then give a following command:
1 |
|
Tip
If you want to run in specific Android device, use below adb devices
command to list all devices in your computer.
1 |
|
1 2 |
|
1 |
|
1 |
|
Error: SDK location not found
You might get a following "SDK location not found" error message:
1 2 3 |
|
SO, you need to and on ANDROID_SDK_ROOT environment variable or create a local.properties file to your android folder in your project. You can find more nice instructions for example here: React Native Android Build Failed
Go to the android directory of your react-native project and create a file called local.properties with this line in macOS:
1 |
|
Where USERNAME is your macOS username or in Windows (check your sdk location, might be different):
1 |
|
You might get also error message that adb is not found, then you need to modify your path to point also to platform-tools folder where adb is.
1 |
|
All ok - hopefully
After you get CLI started successfully, you should see your new app running in the emulator shortly:
Using JavaScript Instead of TypeScript
React Native defaults new applications to TypeScript, but JavaScript may still be used. Files with a .jsx
extension are treated as JavaScript instead of TypeScript, and will not be typechecked. JavaScript modules may still be imported by TypeScript modules, along with the reverse.
Rename your App.tsx
file to App.jsx
and modify your App content as below. Now you can continue using JavaScript (not TypeScript). OR you can modify exercise by yourself to support/use TypeScript, if you want to.
Modify App.jsx
code to include only needed starting code for this project. Now app will render only SafeAreaView
, StatusBar
, View
with one Text
component.
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 |
|
Save your code or double tap R with your keyboard, if emulator isn't refresh automatically.
Load movies and show data
Read React Native Networking material to get familiar with the basic's of Networking. Fetch
is good, when data is JSON based. Of course you can use some 3th party libraries like Axios or Axios npm.
Tip
You can find nice materials about React in left course menu!
Install and use Axios
Install Axios and use it later to load JSON data in your application.
1 |
|
Modify App.js
code and import axios.
1 2 |
|
MovieList component
Create a new MoviesList
function component inside your App.js
.
1 2 3 4 5 |
|
OR you can define your MovieList
component with const:
1 2 3 4 5 |
|
And remember use it in main App
.
1 2 3 4 5 6 7 |
|
Use React hooks to store loaded movies data in your MovieList component.
1 |
|
Tip
Remember import useState
and useEffect
from react library. useEffect
will be used later.
1 |
|
Load movies data with Axios and React effect hooks in your MovieList
.
1 2 3 4 5 6 7 8 9 |
|
Note
Remember use your own API key in above command
Now movies data should be loaded and stored to movies
state in MoviesList
component.
MovieListItem component component
Create a new MovieListItem
function component. This component will render one movie data in the UI. Now it will only display movie title text.
1 2 3 4 5 6 7 |
|
OR create it with const:
1 2 3 4 5 6 7 |
|
Use MovieListItem
component in MoviesList
component rendering. 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 18 19 20 21 22 |
|
Modify your app css-styles.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Double tap R in your emulator. Movie's show titles should be visible now.
Modify MovieListItem
to display more movie data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Tip
Remember import Image
from react-native
.
Test
And now, refresh your app again and try scrolling it's view. Image will be visible at the left and movie data in right.
Add a navigation and a movie details page
Mobile apps are rarely made up of a single screen. Managing the presentation of, and transition between, multiple screens is typically handled by what is known as a navigator.
Read more information React Navigation.
Install React Navigation to your project. Read above documentation and learn which npm packages you need to install. Remember you are now working with React Native CLI project.
1 2 |
|
Create a new MovieListScreen.js
file and copy all App.js
content inside it. Modify App
declaration to be a MovieListScreen
component.
1 |
|
1 |
|
Note
Add { navigation }
to pass navigation
object to MovieListScreen
.
App.js
need to be changed to generate a app navigation. React Navigation uses a stack navigator to manage the navigation history and presentation of the appropriate screen based on the route taken by a user inside the app. Now App
only have a MovieListScreen
, so it will be used, when an application is launched.
Modify App.js
to include navigation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Test your application. It should work the same way as without a navigation, except the navigation bar is now visible. If you got some errors like "RNSScreenStackHeaderConfig"
was not found in the UIManager, just uninstall your app from the emulator/phone and kill Metro Builder. Install your app again, it should work.
Create a new MovieDetailScreen.js
file. This will be used to display information about selected movie from the movie list.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Modify App.js
to include navigation to this MovieDetailScreen
.
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 |
|
Modify MovieListItems
creation in a MoviesList
rendering. Add TouchableHighlight
around MovieListItem
to handle a tap gesture.
1 2 3 4 5 6 7 |
|
Note
Remember add import to TouchableHighlight
and create a following itemPressed
function:
Add itemPressed
function.
1 2 3 |
|
Save and/or double tap R in your emulator and click one of the movie rows. You should see Alert and row number.
Add navigation to MovieDetail
page. Remove alert(index)
line and add navigation to MovieDetail
screen:
1 2 3 4 5 6 |
|
Now above code is in the child component (MoviesList
) and you can't use navigation from there without sending it from the parent component. So, you need to modify MovieListScreen
class to pass navigation
as a props. Modify MovieListScreen
render to pass a main navigation to MoviesList
child component via props.
1 2 3 4 5 6 7 8 9 10 |
|
Now test your app and you should can move between main and detail screens. Of course MovieDetail
screen is still blank.
Note
Read more about navigation here: React Navigation
Show movie details in MovieDetailScreen
You can pass a parameters to the new screen, when you are using a navigation. Modify itemPressed
function to send selected movie data to the MovieDetail
screen.
1 2 3 4 5 6 |
|
Modify MovieDetailScreen
to show movie details. route
object will be passed here via props and it will be used to get movie
data from navigation parameters. Component will render movie poster with Image
component and a few data with Text
component.
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 35 36 |
|
Test
Save and test your app. Now it should work.
Push to GitLab
Test your application in emulator, take screenshots (add those to your project folder) and commit/push your mobile-exercises repository 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.