React Native is a JavaScript framework for writing mobile applications for iOS and Android. It’s based on React and React Native is so popular.

In this tutorial, I’ll show you to build a sample e-commerce application with React Native. If you need, you can check this repo to see the files created throughout this article.

Prerequisites

Before continue reading this tutorial you will need Node.js and npm installed on your machine.

I am going to use Expo for faster development. I’ll explain what is Expo later in this article.

npm install -g expo-cli

Setting up

Building mobile applications for iOS and Android require installing and configuring Xcode or Android Studio. Expo CLI sets up a development environment on your local machine and you can be writing a React Native app within minutes.

Install the Expo client app on your iOS or Android phone and connect to the same wireless network as your computer.

Let’s create a new React Native project:

expo init EcommerceApp
cd EcommerceApp

Going over what we’ll be building in this tutorial

Installing required packages

In this tutorial, we’ll use two libraries:

yarn add react-native-elements
yarn add react-navigation

Once, required packages are installed, just run npm start to start the development server.

And then, you will see the following result:

 

We have two views into the mobile app. We should find a way to move between these views easily.

How the flow will work:

When the application loads we’ll render the products home page and product details page.

Create a src folder. And then create a stack navigator and our routes:

This is how a stack navigator looks like:

So let’s create a stack navigator and our routes:

import { createStackNavigator } from 'react-navigation';
import HomeScreen from './views/Home';
import DetailsScreen from './views/Details';

const AppNavigator = createStackNavigator(
    {
        Home: {
            screen: HomeScreen,
            navigationOptions: { title: 'Home' }
        },
        Details: {
            screen: DetailsScreen,
            navigationOptions: { title: 'Details' }
        }
    },
    {
        initialRouteName: "Home"
    }
);

export default AppNavigator;

We created the HomeScreen and the DetailsScreen. navigationOptions is useful to set up a title for the screen.

initialRouteName option sets the default screen.

If you look at our files, HomeScreen and DetailsScreen are not created yet. Create new folder called views.

Just create a sample React components with React Native’s View and Text:

// src/views/Home.js
import React from 'react';
import { View, Text } from 'react-native';

class HomeScreen extends React.Component {
    render() {
      return (
        <View>
            <Text>Home</Text>
        </View>
      );
    }
}

export default HomeScreen;
// src/views/Details.js
import React from 'react';
import { View, Text } from 'react-native';

class DetailsScreen extends React.Component {
    render() {
      return (
        <View>
            <Text>Details</Text>
        </View>
      );
    }
}

export default DetailsScreen;

Now, remove the content from App.js file. We need to tell our app to use the stack navigator created previously.

import { createAppContainer } from 'react-navigation';
import AppNavigator from './src/AppNavigator';

export default createAppContainer(AppNavigator);

You’ll see this on the screen.

E-Commerce App with React Native

Another challenge before making our app looks awesome is how we move from HomeScreen to DetailsPage.

// src/views/Home.js
import React from 'react';
import { View, Text, Button } from 'react-native';
import { withNavigation } from 'react-navigation';

class HomeScreen extends React.Component {
    render() {
      return (
        <View>
            <Text>Home</Text>
            <Button
                onPress={() => this.props.navigation.navigate('Details')}
                title="Open details page"
            />
        </View>
      );
    }
}

export default withNavigation(HomeScreen);

Great! In the second part of this tutorial, we’ll make our app looks great. And most essential to look like an ‘real’ e-commerce app.

prevChapter 1 of 2