Skip to main content
This tutorial was written by Claude Code (an AI) and has not yet been reviewed. Follow along with caution. If the tutorial was helpful or a specific part was not clear/correct, please provide feedback at the bottom of the page. Thank you.

Introduction

React Native is a popular framework for building native mobile applications using React. It’s an excellent choice for creating mobile storefronts with CoCart because it allows you to build iOS and Android apps from a single codebase while maintaining native performance and user experience. This guide will walk you through setting up a React Native project configured to work with CoCart API.

Why React Native for Headless Commerce?

  • Cross-platform - Build for iOS and Android from one codebase
  • Native performance - Real native components, not webviews
  • React ecosystem - Use familiar React patterns and libraries
  • Hot reloading - Fast development with instant feedback
  • Large community - Extensive third-party packages and support
  • Cost effective - One team can build for multiple platforms

Prerequisites

  • Node.js 18 or higher
  • A WordPress site with WooCommerce installed
  • CoCart plugin installed and activated
  • Basic knowledge of React and JavaScript
  • For iOS development: macOS with Xcode installed
  • For Android development: Android Studio installed

Development Environment Setup

Before creating your project, set up your development environment:
For detailed environment setup, refer to React Native Environment Setup.

Creating a New React Native Project

Create a new React Native project using the official CLI:
Navigate to your project:

Project Structure

Your React Native project will have this structure:
Create the source directory structure:

Installing Essential Dependencies

Install the necessary packages for a complete e-commerce app:
For iOS, install pods:

Environment Configuration

Install the config package:
Create a .env file in your project root:
Add .env to your .gitignore:
Create a .env.example:

Creating the CoCart API Client

Create a centralized API client at src/api/cocart.ts:
We are currently building out this client, so for now just make standard fetch/axios requests to the CoCart API endpoints as needed.

Setting Up React Query

Create a query client configuration at src/api/queryClient.ts:

Creating Custom Hooks

Create a cart hook at src/hooks/useCart.ts:
Create a products hook at src/hooks/useProducts.ts:

Setting Up Navigation

Create the navigation structure at src/navigation/AppNavigator.tsx:

Creating Example Screens

Create a basic home screen at src/screens/HomeScreen.tsx:
Create a placeholder for the cart screen at src/screens/CartScreen.tsx:
Create placeholder screens:

Updating App.tsx

Update your root App.tsx file:

Running Your App

Testing on Physical Devices

Building for Production

App Store Submission

Performance Optimization

Image Optimization

  • Use react-native-fast-image for better image performance
  • Cache images appropriately
  • Use appropriate image sizes

List Optimization

  • Use FlatList with proper keyExtractor
  • Implement getItemLayout for fixed-height items
  • Use maxToRenderPerBatch and windowSize props

Code Splitting

Troubleshooting

Metro Bundler Issues

CORS Errors

If you encounter CORS errors, configure WordPress to allow cross-origin requests. See CORS documentation.

Network Issues

Next Steps

Now that your React Native app is set up with CoCart:
  1. Implement full product detail screens
  2. Complete cart functionality (update quantities, remove items)
  3. Add checkout flow
  4. Implement user authentication with JWT
  5. Add push notifications for order updates
  6. Implement offline support with AsyncStorage
  7. Add analytics and crash reporting

Resources