React Native is a cross-platform mobile app framework that allows you to build native apps for iOS and Android using JavaScript. It was developed by Meta Platforms, Inc. and it is currently among the most popular JavaScript frameworks for building native apps with a huge active community behind it.
Medusa is an open source headless commerce platform that allows you to create online shopping stores in a few minutes. It includes every feature a store needs such as order management, customers, payments, products, discounts, and a lot more.
In this tutorial, you are building a React Native mobile commerce application with Medusa. For this part, you are going to create two screens, one for all products and the other for the product info.
You can also find the source code of the React Native app on the GitHub
Prerequisites
Before you start with the tutorial make sure you have Node.js v14 or greater installed on your machine.
Set Up Medusa Server
The first step is to setup the Medusa server, where the backend and APIs are handled.
You can install the Medusa CLI on your machine by running the following command:
1npm install -g @medusajs/medusa-cli
Once the CLI is installed successfully, run the following command to create a Medusa project:
1medusa new my-medusa-store --seed
The Copy to clipboard--seed
option is used to add dummy data such as products and users to the store.
Change to the newly-created directory Copy to clipboardmy-medusa-store
and run the following command to start the medusa server:
1npm start
It is recommended to add a storage plugin to be able to add products with images in Medusa. You can use MinIO, AWS S3, or Spaces.
Set Up Medusa Admin
Medusa has a very powerful admin dashboard where you can manage your products, payments, transaction, and more. This is very easy to setup however it is optional, so if you want you can skip this section.
In a separate directory, clone the Medusa Admin:
1git clone https://github.com/medusajs/admin medusa-admin
Once it is cloned, you should see a new directory named Copy to clipboardmedusa-admin
. Navigate to the new directory and run the following command to install the dependencies of the project:
1npm install
Run Admin** **Panel
Finally, make sure that the Medusa server is still running and start the admin panel server by running the following command:
1npm run develop
Now, open your browser and navigate to Copy to clipboardlocalhost:7000
and you should see the login page for admin panel. Login in to the admin with the below credentials.
- Email: admin@medusa-test.com
- Password: supersecret
The Copy to clipboard--seed
option you used earlier adds an admin user with the email and password
Create Products
Once you are logged in successfully, choose Products from sidebar and you should see the list of products in your store.
If you are experiencing connection issues, it's most likely because of CORS issues. Check here how to fix it.
You can also create a new product by clicking on the "New Product" button. Add information for your product such as a name, description, handle, variants, images, prices, and a lot more.
Set Up React Native Ecommerce Project
Now that you have the store backend and admin panel ready it is time to start working on the react native ecommerce app.
In this tutorial, you are using Expo CLI to build the app. Run the following command to install the Expo CLI:
1npm install -g expo-cli
Once the CLI is installed successfully, run the following command to create a new react native ecommerce project:
1expo init
You will be promoted with some questions. You can follow the below code for the answers:
12345678910111213141516What would you like to name your app? … medusa-storeChoose a template: › blank a minimal app as clean as an empty canvasDownloaded template.🧶 Using Yarn to install packages. Pass --npm to use npm instead.Installed JavaScript dependencies.✅ Your project is ready!To run your project, navigate to the directory and run one of the following yarn commands.- cd medusa-store- yarn start # you can open iOS, Android, or web from here, or run them directly with the commands below.- yarn android- yarn ios- yarn web
Install Dependencies
Once the project is created successfully you should see a new directory named Copy to clipboardmedusa-store
. Navigate to the new directory and run the following command to install a few other dependencies:
1expo install react-native-screens react-native-router-flux react-native-reanimated rn-responsive-screen react-native-safe-area-context @expo/vector-icons react-native-gesture-handler axios
- Copy to clipboard
react-native-screens
is used to expose native navigation container components to React Native. - Copy to clipboard
react-native-router-flux
provides API that helps users navigate between screens. - Copy to clipboard
react-native-reanimated
creates smooth animations and interactions that run on the UI thread. - Copy to clipboard
rn-responsive-screen
is a small package used for responsiveness in the app. - Copy to clipboard
react-native-safe-area-context
is a flexible way to handle safe areas. - Copy to clipboard
react-native-gesture-handler
provides native-driven gesture management APIs for building the best possible touch-based experiences. - Copy to clipboard
axios
is a promise-based HTTP Client to easily send requests to REST APIs and perform CRUD operations. - Copy to clipboard
@expo/vector-icons
includes popular icons sets which you can use in the app.
Run Expo Development Server
After the packages are installed successfully, start the development server by running the following:
1expo start
You can either scan the QR code using your device or run the app on an Android/iOS simulator. Once the app is shown on your mobile, you should see a similar screen.
This is a basic react native code in the Copy to clipboardApp.js
file.
Set Up Routes
In this section, you’ll set up different routes in your app.
Before setting up the routes, you have to create a few screens. Create a new folder named Copy to clipboardscreens
and inside it create a new file named Copy to clipboardProducts.js
.
Inside Copy to clipboardProducts.js
insert the following code:
123456789101112131415161718import { StyleSheet, Text, View } from "react-native";export default function Products() {return (<View style={styles.container}><Text>Product Screen!</Text></View>);}const styles = StyleSheet.create({container: {flex: 1,backgroundColor: "#fff",alignItems: "center",justifyContent: "center",},});
For now it contains a very simple Copy to clipboardText
component.
Now that you have a screen setup, you can continue to add routes to the project. Replace the code inside of the Copy to clipboardApp.js
with the following:
123456789101112import { Router, Scene, Stack } from "react-native-router-flux";import Products from "./screens/Products";export default function App() {return (<Router><Stack key="root"><Scene key="products" component={Products} hideNavBar /></Stack></Router>);}
In the above code, you are using Copy to clipboardreact-native-router-flux
to create the navigation. Copy to clipboardRouter
is used as a parent component and each Copy to clipboardScene
represents one screen. For now you have just one screen.
Save the file and you might see an error similar to this.
1Error: Requiring module "node_modules/react-native-reanimated/src/Animated.js", which threw an exception: Error: Reanimated 2 failed to create a worklet, maybe you forgot to add Reanimated's babel plugin?
It is because that Copy to clipboardreact-native-router-flux
uses Copy to clipboardreact-native-reanimated
and in order to make it work you need to add it to Copy to clipboardbabel.config.js
. Open the babel file from your directory and add the below line after Copy to clipboardpresents
:
1plugins: ["react-native-reanimated/plugin"],
Save the file and restart the server with the following command:
1expo start -c
The option Copy to clipboard-c
clears the cache before running the server.
If you see the error “Invariant Violation: Tried to register two views with the same name RNGestureHandlerButton”, delete the Copy to clipboardnode_modules
directory and use Yarn to re-install the dependencies.
Products List Screen
Create a new folder in the root directory named Copy to clipboardcomponents
. In the Copy to clipboardcomponents
folder create 3 files. Copy to clipboardButton.js
, Copy to clipboardProductCard.js
, and Copy to clipboardHeader.js
.
In the Copy to clipboardButton.js
file insert the following code to create a basic button component:
1234567891011121314151617181920import { View, Text, StyleSheet } from "react-native";import React from "react";import { widthToDp } from "rn-responsive-screen";export default function Button({ title, onPress, style, textSize }) {return (<View style={[styles.container, style]}><Textstyle={[styles.text, { fontSize: textSize ? textSize : widthToDp(3.5) }, ]}onPress={onPress}>{title}</Text></View>);}const styles = StyleSheet.create({container: {backgroundColor: "#C37AFF",
Similarly in the Copy to clipboardHeader.js
insert the following code to create a simple header component:
1234567891011121314151617181920import { View, Image, StyleSheet, Text } from "react-native";import React from "react";export default function Header({ title }) {return (<View style={styles.container}><Imagesource={{uri: "https://user-images.githubusercontent.com/7554214/153162406-bf8fd16f-aa98-4604-b87b-e13ab4baf604.png",}}style={styles.logo}/><Text style={styles.title}>{title}</Text></View>);}const styles = StyleSheet.create({container: {flexDirection: "row",justifyContent: "space-between",
The last one is Copy to clipboardProductCard.js
. It is the main component in which you render the product data:
1234567891011121314151617181920import { View, Text, Image, StyleSheet } from "react-native";import React from "react";import { widthToDp, heightToDp } from "rn-responsive-screen";import Button from "./Button";export default function ProductCard({ key, product }) {return (<View style={styles.container} key={key}><Imagesource={{uri: product.thumbnail,}}style={styles.image}/><Text style={styles.title}>{product.title}</Text><Text style={styles.category}>{product.handle}</Text><View style={styles.priceContainer}><Text style={styles.price}>${product.variants[0].prices[1].amount / 100}</Text>
In the above code, the product price is divided by 100 because in Medusa the prices are hosted on the server without decimals.
Create URL Constant
Create a new folder named Copy to clipboardconstants
and inside it create a new file named Copy to clipboardurl.js
with the following content:
123const baseURL = "http://127.0.0.1:9000";export default baseURL;
In the above code, you define your Medusa server’s base URL. To be able to connect from your device to the local server, you must change the value of Copy to clipboardbaseURL
to your machine’s IP address. You can refer to this guide to learn how to find your IP address.
Products Screen
That’s it for the components. Now replace the code in the Copy to clipboardProducts.js
with the following:
1234567891011121314151617181920import { ScrollView, StyleSheet,TouchableOpacity, View } from "react-native";import React, { useEffect, useState } from "react";import ProductCard from "../components/ProductCard";import { widthToDp } from "rn-responsive-screen";import axios from "axios";import Header from "../components/Header";import { Actions } from "react-native-router-flux";import baseURL from "../constants/url";export default function Products() {const [products, setProducts] = useState([]);function fetchProducts() {axios.get(`${baseURL}/store/products`).then((res) => {setProducts(res.data.products);});}useEffect(() => {fetchProducts();
In the code above, you call Copy to clipboardfetchProducts
when the screen loads using Copy to clipboarduseEffect
. In the Copy to clipboardfetchProducts
function, you use Copy to clipboardaxios
to fetch the products from the Medusa server and save it in the state.
Once you fetch the products, you render them using the Copy to clipboardProductCard
component.
Run the App
Save the file and make sure that Expo and the Medusa server are running. Then, open the app on your device and you should see on the home screen the products from your Medusa server.
Product Info Screen
In this section, you’ll create the Product Info screen where the user can see more details about the product.
In the Copy to clipboardscreens
directory, create a new file named Copy to clipboardProductInfo.js
and for now you can use it to render a simple Copy to clipboardText
component:
12345678910import { View, Text } from "react-native";import React from "react";export default function ProductInfo() {return (<View><Text>Product Info Screen</Text></View>);}
Then, add the import Copy to clipboardProductInfo
at the top of Copy to clipboardApp.js
:
1import ProductInfo from "./screens/ProductInfo";
And add a new Copy to clipboardScene
component below the existing Copy to clipboardScene
component in the returned JSX:
1<Scene key="ProductInfo" component={ProductInfo} hideNavBar />
In the Copy to clipboardcomponents
directory, create a new directory named Copy to clipboardProductInfo
and create inside it Copy to clipboardImage.js
with the following content:
1234567891011121314151617181920import { View, TouchableOpacity, Image, StyleSheet } from "react-native";import React, { useEffect, useState } from "react";import { widthToDp } from "rn-responsive-screen";export default function Images({ images }) {const [activeImage, setActiveImage] = useState(null);useEffect(() => {setActiveImage(images[0].url);}, []);return (<View style={styles.imageContainer}><Image source={{ uri: activeImage }} style={styles.image} /><View style={styles.previewContainer}>{images.map((image, index) => (<TouchableOpacitykey={index}onPress={() => {setActiveImage(image.url);
In the above component you display a main big image and below it the rest of the product images as thumbnails. When the user press on one of the thumbnail images it is set as the active image and displayed as the main image.
In the Copy to clipboardProducts.js
file, replace the map function in the returned JSX with the following:
123456{products.map((product) => (<TouchableOpacity key={product.id} onPress={() => Actions.ProductInfo({ productId: product.id })}><ProductCard product={product} /></TouchableOpacity>))}
You add a Copy to clipboardTouchableOpacity
that navigates the user to the product info screen when they click on a product.
Using Copy to clipboardreact-router-flux
you pass the product ID from the home screen to the product info screen.
Then, replace the code in Copy to clipboardProductInfo.js
with the following:
1234567891011121314151617181920import { View, Text, ScrollView,TouchableOpacity, StyleSheet } from "react-native";import React, { useState, useEffect } from "react";import axios from "axios";import { SafeAreaView } from "react-native-safe-area-context";import Images from "../components/ProductInfo/Image";import baseURL from "../constants/url";import { Actions } from "react-native-router-flux";import { Ionicons } from "@expo/vector-icons";export default function ProductInfo({ productId }) {const [productInfo, setproductInfo] = useState(null);useEffect(() => {axios.get(`${baseURL}/store/products/${productId}`).then((res) => {setproductInfo(res.data.product);});}, []);return (<SafeAreaView style={styles.container}>
To briefly explain the code snippet:
- First you import all necessary components.
- Then you fetch the product data in useEffect function and save it in the state.
- Finally, you display the images using the Copy to clipboard
Images
component.
Test Product Info Screen
Open the app now and click on any product on the home screen. A new screen will open showing the product’s images.
Now, you’ll display the product’s information.
In the Copy to clipboardcomponents
folder, inside the Copy to clipboardProductInfo
directory create a new file named Copy to clipboardMetaInfo.js
with the following content:
1234567891011121314151617181920import { View, Text, StyleSheet } from "react-native";import React, { useState } from "react";import { height, heightToDp } from "rn-responsive-screen";export default function MetaInfo({ product }) {const [activeSize, setActiveSize] = useState(0);return (<View style={styles.container}><View style={styles.row}><Text style={styles.title}>{product.title}</Text><View><Text style={styles.price}>${product.variants[0].prices[1].amount / 100}</Text><Text style={styles.star}>⭐⭐⭐</Text></View></View><Text style={styles.heading}>Available Sizes</Text><View style={styles.row}>{product.options[0].values.map((size, index) => (
In the above component, you render the product title, price, description and variants.
For the product variant, you map all the variants and when a user press on one of them, you set that variant as active.
Save the Copy to clipboardMetaInfo.js
file and import it at the top of Copy to clipboardscreens/ProductInfo.js
:
1import MetaInfo from "../components/ProductInfo/MetaInfo";
Then, in the returned JSX add the Copy to clipboardMetaInfo
component below the Copy to clipboardImages
component:
1<MetaInfo product={productInfo} />
Test New Changes in Product Info Screen
Save the changes and check the app now. The product info screen now shows details about the product.
What’s Next?
This article gives you the basis for creating a Medusa and React Native apps. Here are some more functionalities you can add using Medusa:
- Add a cart and allow adding products to cart.
- Add a payment provider using Stripe.
- Add a search engine using MeiliSearch.
- Check out the documentation for what more you can do with Medusa.
Should you have any issues or questions related to Medusa, then feel free to reach out to the Medusa team via Discord. Also, check out our Next.js-based Storefront template.