Getting Started with React Native and Expo using Hooks in 2020

Published on Jan 17, 2020

17 min read

EXPO

We live in the world of a variety of mobile devices majorly dominated by two platforms, iOS, and Android. It is a two-horse race and I am sure we can all agree on that. Building a mobile application is not an easy task though.

For iOS, you write code using Objective-C or Swift and for Android, you will find yourself using Java or Kotlin. Apart from different programming languages used to create a mobile that can run on each of the two platforms, the toolchains are entirely different too for both of these mobile platforms.

Many modern-day developers use a specific set of technology that is used to build web applications: HTML, CSS, and JavaScript. Different frameworks fall under the category commonly known as Hybrid applications. You can use almost one set of source code for developing the application for both iOS and Android platforms.

In recent years, hybrid frameworks have evolved coming from web view to use native APIs. This cross-platform approach of developing a mobile application comes with its own pros and cons.

One great option that falls under the umbrella of cross-platform development is React Native. Developed and used by Facebook as well others such as Tesla, Walmart, Uber Eats, Instagram, Discord, Wix and so on. React Native is based on Facebook’s web library ReactJS.

What this tutorial is about?

🔗

React Hooks are available since the release version 16.8.x. In this tutorial, you are going to get a quick introduction on how to use them in a React Native app. These functions allow using React state and a component’s lifecycle methods in a functional component. If you are familiar with React, you know that the functional component has been called as a functional stateless component since the introduction of classes, but not anymore.

Previously, a class component allowed you to have a local state. Using React Hooks, there is no requirement to refactor a class component React Native into a functional component only because you want to introduce local state or lifecycle methods in that component. However, they do not work with classes. React provides a few built-in Hooks such as useState and useEffect. You can also create your Hooks to re-use to manage state between different components.

Table of contents

🔗
  • Getting Started
  • The entry point of a React Native app
  • Setting up a stack navigation
  • Adding the second screen to the stack navigator
  • Adding a Floating Button component
  • Adding a custom header component
  • Implementing Hooks
  • Adding a FlatList component to render notes
  • Using Navigation parameters to update the state
  • Running the app
  • Conclusion

Getting started

🔗

To quickly create a React Native app, let us use a tool called Expo. It is a managed development toolset that provides a client to preview and make changes to React Native apps using JavaScript. You do not need tools such as Xcode or Android Studio to get started.

To generate a new app, open a terminal window and enter the following command to install the command-line tool provided by Expo itself.

npm install -g expo-cli

Next, step is to run expo init command and choose the default template blank.

# generate a new app
expo init expo-rnHooks
# make sure to navigate inside the project directory
cd expo-rnHooks

Once the project directory is generated, navigate inside it. The demo you are going to build requires the use of a navigation pattern between two screens. The first screen is going to display a list of items and through the second screen, you can add an item to the list. This is a typical stack navigation pattern and using the react-navigation library, you can add this to your React Native app.

The react-navigation library is a third party library that needs to be installed in a React Native or Expo app separately as a dependency. You can either use npm or yarn but I am going to stick with yarn. Each navigational pattern comes as a dependency too since the demo requires only one pattern, let us install that too.

The third library you are going to install is called react-native-paper that will provide a collection of custom UI components based on Material Design that you can integrate directly. Go back to the terminal window and execute the following command.

yarn add react-navigation react-navigation-stack
react-native-paper @react-native-community/masked-view

React Navigation is made up of some core utilities and those are then used by navigators to create the navigation structure in your app. After the above step, Expo requires you to configure these core utilities as dependencies.

expo install react-navigation
react-native-gesture-handler
react-native-reanimated react-native-screens
react-navigation-stack

That's all for the setup. Let us build something.

The entry point of a React Native app

🔗

The App.js file in the generated app structure is what initializes the Expo app. In other words, it is the entry point of the development process. By default, it displays a text message and uses a functional component for that. Open the App.js file and you are going to get the following screen component file.

1import React from 'react';
2import { StyleSheet, Text, View } from 'react-native';
3
4export default function App() {
5 return (
6 <View style={styles.container}>
7 <Text>Open up App.js to start working on your app!</Text>
8 </View>
9 );
10}
11
12const styles = StyleSheet.create({
13 container: {
14 flex: 1,
15 backgroundColor: '#fff',
16 alignItems: 'center',
17 justifyContent: 'center'
18 }
19});

Components are the visual elements that you see on the screen in a React Native app. The three major components to look for in the above code snippet are:

  • View
  • Text
  • StyleSheet

A View component is the basic building block in a React Native component file. It maps to fundamental native iOS (UIView) and Android (View) components, hence its name. It puts a container element that supports layout styling with flexbox and other styles using a JavaScript object called StyleSheet. Hence, it can be said that View components are primarily used for styling and the layout of children elements.

The StyleSheet component in React Native provides an API to create styles inside the component file. It takes a JavaScript object as it does above, and returns a new StyleSheet object from it. There are no classes or IDs in React Native like in web development. To create a new style object, you can use the StyleSheet.create() method.

The Text component is in many ways just like the View component, except that it is specifically available to display text. Also, like the View component, it supports styling.

To see the default app in action, start the development server from the terminal window expo start. Either using a simulator or a real device (make sure it has an Expo client installed from the app store) you can test the app.

Setting up a stack navigation

🔗

The react-navigation-stack library provides an inbuilt function that returns a React component. This function, createStackNavigator takes a route configuration object and an options object (which is optional).

The react-navigation library provides a function called createAppContainer that returns a React component. It takes React component created by the createStackNavigator as a parameter and is be directly exported to App.js to be used as our App's root component.

To create the first route, you need to create the first screen. Create a new file called ViewNotes.js inside src/screens directory. This screen is going to be served as the first or home screen of the app. Right now, let us add some mock components and later we will add UI component to reflect the demo app.

1import React from 'react';
2import { StyleSheet, View } from 'react-native';
3import { Text } from 'react-native-paper';
4
5function ViewNotes() {
6 return (
7 <View style={styles.container}>
8 <View style={styles.titleContainer}>
9 <Text style={styles.title}>You do not have any notes</Text>
10 </View>
11 </View>
12 );
13}
14
15const styles = StyleSheet.create({
16 container: {
17 flex: 1,
18 backgroundColor: '#fff',
19 paddingHorizontal: 10,
20 paddingVertical: 20
21 },
22 titleContainer: {
23 alignItems: 'center',
24 justifyContent: 'center',
25 flex: 1
26 },
27 title: {
28 fontSize: 20
29 }
30});
31
32export default ViewNotes;

Next, create a new file called index.js inside src/navigation/ with the following code snippet.

1import { createAppContainer } from 'react-navigation';
2import { createStackNavigator } from 'react-navigation-stack';
3import ViewNotes from '../screens/ViewNotes';
4
5const StackNavigator = createStackNavigator(
6 {
7 ViewNotes: {
8 screen: ViewNotes
9 }
10 },
11 {
12 initialRouteName: 'ViewNotes',
13 headerMode: 'none'
14 }
15);
16
17export default createAppContainer(StackNavigator);

In the above code snippet, the parameters such as initialRouteName and headerMode are passed as the optional object properties. The first object contains the route configuration.

To see this in action, open the App.js file, import the navigator created above as well as PaperProvider component from react-native-paper. This provider is going to wrap the navigator and provides the theme to all the components in the framework. I

1import React from 'react';
2import { Provider as PaperProvider } from 'react-native-paper';
3import AppNavigator from './src/navigation';
4
5export default function App() {
6 return (
7 <PaperProvider>
8 <AppNavigator />
9 </PaperProvider>
10 );
11}

Make sure the development server is running. You are going to get the following output in an Expo client.

Adding the second screen to the stack navigator

🔗

To complete the navigation process, let us set up the other screen with some mock text to display. Inside src/screens/ create another file called AddNotes.js and the following code snippet.

1import React from 'react';
2import { StyleSheet, View } from 'react-native';
3import { Text } from 'react-native-paper';
4
5function AddNotes() {
6 return (
7 <View style={styles.container}>
8 <View style={styles.titleContainer}>
9 <Text style={styles.title}>Add Notes modal screen</Text>
10 </View>
11 </View>
12 );
13}
14
15const styles = StyleSheet.create({
16 container: {
17 flex: 1,
18 backgroundColor: '#fff',
19 paddingHorizontal: 10,
20 paddingVertical: 20
21 },
22 titleContainer: {
23 alignItems: 'center',
24 justifyContent: 'center',
25 flex: 1
26 },
27 title: {
28 fontSize: 20
29 }
30});
31
32export default AddNotes;

Open the navigation/index.js file and modify the stack navigator.

1import { createAppContainer } from 'react-navigation';
2import { createStackNavigator } from 'react-navigation-stack';
3import ViewNotes from '../screens/ViewNotes';
4import AddNotes from '../screens/AddNotes';
5
6const StackNavigator = createStackNavigator(
7 {
8 ViewNotes: {
9 screen: ViewNotes
10 },
11 AddNotes: {
12 screen: AddNotes
13 }
14 },
15 {
16 initialRouteName: 'ViewNotes',
17 headerMode: 'none',
18 mode: 'modal'
19 }
20);
21
22export default createAppContainer(StackNavigator);

Do note that in the options object, adds a mode for stack navigator to modal. A modal is like a popup and displays the content but temporarily blocks the interaction from the primary screen, which in this case is ViewNotes screen. To access the second screen you still require to add a way to navigate.

Adding a Floating Button component

🔗

Since react-native-paper provides cross-platform components to add to the app. In this section, let us add a floating button on the ViewNotes screen that can be used to navigate to the AddNotes screen. Import the component from the UI library.

1import { Text, FAB } from 'react-native-paper';

Next, modify the return function and a FAB component as well as corresponding styles to position it at the bottom of the screen.

1function ViewNotes({ navigation }) {
2 return (
3 <View style={styles.container}>
4 <View style={styles.titleContainer}>
5 <Text style={styles.title}>You do not have any notes</Text>
6 </View>
7 <FAB
8 style={styles.fab}
9 small
10 icon="plus"
11 label="Add new note"
12 onPress={() => navigation.navigate('AddNotes')}
13 />
14 </View>
15 );
16}
17
18const styles = StyleSheet.create({
19 container: {
20 flex: 1,
21 backgroundColor: '#fff',
22 paddingHorizontal: 10,
23 paddingVertical: 20
24 },
25 titleContainer: {
26 alignItems: 'center',
27 justifyContent: 'center',
28 flex: 1
29 },
30 title: {
31 fontSize: 20
32 },
33 fab: {
34 position: 'absolute',
35 margin: 20,
36 right: 0,
37 bottom: 10
38 }
39});

In the Expo client you are going to get the following output:

Also, when you click the FAB button, it will navigate you to the AddNotes screen.

This is done by navigation props from react-navigation. Using navigation.navigate as the value of the button press prop onPress, the app will navigate to the screen with its name passed as the second parameter.

1onPress={() => navigation.navigate('AddNotes')}

Adding a custom header component

🔗

In this section, let us build a custom header component that is reusable for both of the screens currently residing in the app. Inside the directory src/components/ create a new file called Header.js file.

Import the following components from react-native and react-native-paper.

1import React from 'react';
2import { View, StyleSheet } from 'react-native';
3import { Appbar, Title } from 'react-native-paper';

The Appbar is a component that displays items in a bar. Each of the items can have an action associated but for the demo app, you only require it to display a title. Add the following code snippet that consists of the component as well as the corresponding styles.

The Header component is going to accept one prop titleText that is the title of a specific screen.

1function Header({ titleText }) {
2 return (
3 <Appbar.Header style={styles.headerContainer}>
4 <View style={styles.container}>
5 <Title style={styles.title}>{titleText}</Title>
6 </View>
7 </Appbar.Header>
8 );
9}
10
11const styles = StyleSheet.create({
12 headerContainer: {
13 backgroundColor: '#60DBC5'
14 },
15 container: {
16 flex: 1,
17 justifyContent: 'center',
18 alignItems: 'center'
19 },
20 title: {
21 color: '#2E7166'
22 }
23});
24
25export default Header;

Import this component in ViewNotes.js and modify the contents of the component file in order to display the header.

1// add the following statement
2import Header from '../components/Header';
3
4// modify ViewNotes component
5function ViewNotes({ navigation }) {
6 return (
7 <>
8 <Header titleText="Simple Note Taker" />
9 <View style={styles.container}>
10 <View style={styles.titleContainer}>
11 <Text style={styles.title}>You do not have any notes</Text>
12 </View>
13 <FAB
14 style={styles.fab}
15 small
16 icon="plus"
17 label="Add new note"
18 onPress={() => navigation.navigate('AddNotes')}
19 />
20 </View>
21 </>
22 );
23}

The following is going to be the output.

Similarly, modify the AddNotes.js file.

1// add the following statement
2import Header from '../components/Header';
3
4// modify AddNotes component
5function AddNotes() {
6 return (
7 <>
8 <Header titleText="Add a new note" />
9 <View style={styles.container}>
10 <View style={styles.titleContainer}>
11 <Text style={styles.title}>Add Notes modal screen</Text>
12 </View>
13 </View>
14 </>
15 );
16}

Here is the output:

Implementing Hooks

🔗

To clearly understand how functional components could be leveraged to manage a state’s component, let us try to go through one of the most basic examples by leveraging one of the few built-in Hooks like useState.

Open ViewNotes.js file and start by importing useState from the React library.

1import React, { useState } from 'react';

Let us an array to store and display all the notes. Using the array later as the value to the FlatList component, you can easily render each note. In a functional component, you can define a default state variable as shown below.

1function ViewNotes({ navigation }) {
2 const [notes, setNotes] = useState([]);
3
4 // ...
5}

React preserves the state between all the re-rendering that happens. The hook useState returns a pair of values. In the above snippet, the first one being the notes which holds the current value of an empty array (by default) and the second, setNotes is a function that lets you update the current value or in the out case, add items to the array.

To add items to the array, let us create a helper method called addNotes.

1const addNote = note => {
2 note.id = notes.length + 1;
3 setNotes([...notes, note]);
4};

Adding a FlatList component to render notes

🔗

When the array notes is empty, let us display a text message that indicates that there is no item in the list otherwise render a FlatList component. To do this, you have to import the component itself first.

The component FlatList is an efficient way to create scrolling data lists in a React Native app. It has a simple API to work with and is more efficient and preferment with a large amount of information to display in comparison to its alternate.

1import { StyleSheet, View, FlatList } from 'react-native';
2import { Text, FAB, List } from 'react-native-paper';

Next, modify the JSX of the ViewNotes component. Do take note that when navigating to AddNotes screen, you have to pass it as a prop. This can be done by passing it as the second parameter to navigation.navigate function.

1return (
2 <>
3 <Header titleText="Simple Note Taker" />
4 <View style={styles.container}>
5 {notes.length === 0 ? (
6 <View style={styles.titleContainer}>
7 <Text style={styles.title}>You do not have any notes</Text>
8 </View>
9 ) : (
10 <FlatList
11 data={notes}
12 renderItem={({ item }) => (
13 <List.Item
14 title={item.noteTitle}
15 description={item.noteValue}
16 descriptionNumberOfLines={1}
17 titleStyle={styles.listTitle}
18 />
19 )}
20 keyExtractor={item => item.id.toString()}
21 />
22 )}
23 <FAB
24 style={styles.fab}
25 small
26 icon="plus"
27 label="Add new note"
28 // add a second parameter object
29 onPress={() =>
30 navigation.navigate('AddNote', {
31 addNote
32 })
33 }
34 />
35 </View>
36 </>
37);

From the above snippet, observe that there are three primary props that a FlatList component requires to display a list of data:

  • data: an array of data that is used to create a list. Generally, this array is built of multiple objects.
  • renderItem: is a function that takes an individual element from the data array and renders it on the UI.
  • keyExtractor: it tells the list of data to use the unique identifiers or id for an individual element.

Also, add the listTitle inside the StyleSheet object.

1listTitle: {
2 fontSize: 20;
3}

Using Navigation parameters to update the state

🔗

Since there are no notes, for now, let us modify the AddNotes screen to make it functional. This screen is responsible to add a note to the ViewNotes screen. Start by modifying the existing import statements.

1import React, { useState } from 'react';
2import { View, StyleSheet } from 'react-native';
3import { IconButton, TextInput, FAB } from 'react-native-paper';

Using the hook useState the component is going to hold the value of each note's title and its description as noteTitle and noteValue.

1function AddNote({ navigation }) {
2 const [noteTitle, setNoteTitle] = useState('');
3 const [noteValue, setNoteValue] = useState('');
4
5 // ...
6}

The IconButton component from react-native-paper is going to be used to close the modal. After that add two input fields using TextInput that are going to take the user value for the title of the note and its description.

Lastly, using a FAB component, the user can submit the form. This component is going to be temporarily disabled of there is no title provided for the note. It can be done by using the disabled prop.

On clicking this button the component using navigation props is going to perform to actions simultaneously. It is going to save the note's title and its description as well as perform an action to go back to the ViewNotes screen.

Here is the complete AddNotes code snippet along with corresponding styles.

1function AddNote({ navigation }) {
2 const [noteTitle, setNoteTitle] = useState('');
3 const [noteValue, setNoteValue] = useState('');
4
5 function onSaveNote() {
6 navigation.state.params.addNote({ noteTitle, noteValue });
7 navigation.goBack();
8 }
9 return (
10 <>
11 <Header titleText="Add a new note" />
12 <IconButton
13 icon="close"
14 size={25}
15 color="white"
16 onPress={() => navigation.goBack()}
17 style={styles.iconButton}
18 />
19 <View style={styles.container}>
20 <TextInput
21 label="Add Title Here"
22 value={noteTitle}
23 mode="outlined"
24 onChangeText={setNoteTitle}
25 style={styles.title}
26 />
27 <TextInput
28 label="Add Note Here"
29 value={noteValue}
30 onChangeText={setNoteValue}
31 mode="flat"
32 multiline={true}
33 style={styles.text}
34 scrollEnabled={true}
35 returnKeyType="done"
36 blurOnSubmit={true}
37 />
38 <FAB
39 style={styles.fab}
40 small
41 icon="check"
42 disabled={noteTitle == '' ? true : false}
43 onPress={() => onSaveNote()}
44 />
45 </View>
46 </>
47 );
48}
49
50const styles = StyleSheet.create({
51 container: {
52 flex: 1,
53 backgroundColor: '#fff',
54 paddingHorizontal: 20,
55 paddingVertical: 20
56 },
57 iconButton: {
58 backgroundColor: 'rgba(46, 113, 102, 0.8)',
59 position: 'absolute',
60 right: 0,
61 top: 40,
62 margin: 10
63 },
64 title: {
65 fontSize: 24,
66 marginBottom: 20
67 },
68 text: {
69 height: 300,
70 fontSize: 16
71 },
72 fab: {
73 position: 'absolute',
74 margin: 20,
75 right: 0,
76 bottom: 0
77 }
78});
79
80export default AddNote;

Here is the output you are going to get when navigating to the AddNotes screen.

Running the app

🔗

The demo app is complete and ready to be tested. In the Expo client image below, and you can find a demo for adding a note and rendering the note.

Conclusion

🔗

If you are getting started in React Native development, Expo as a toolkit can serve you well in your journey. Instead of dwelling much into iOS and Android development setup which can be overwhelming at the start, I'd recommend the least possible amount of tooling and incline more towards learning the core APIs and fundamentals of React Native.

The way the Expo is being maintained and adding support for Web and universal apps, it going to be an important part of the journey.

Originally published at Heartbeat.fritz.ai


More Posts

Browse all posts

Aman Mittal author

I'm a software developer and a technical writer. On this blog, I write about my learnings in software development and technical writing.

Currently, working maintaining docs at 𝝠 Expo. Read more about me on the About page.


Copyright ©  2019-2024 Aman Mittal · All Rights Reserved.