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:Project Structure
Your React Native project will have this structure:Installing Essential Dependencies
Install the necessary packages for a complete e-commerce app:Environment Configuration
Install the config package:.env
file in your project root:
.env
to your .gitignore
:
.env.example
:
Creating the CoCart API Client
Create a centralized API client atsrc/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 atsrc/api/queryClient.ts
:
Creating Custom Hooks
Create a cart hook atsrc/hooks/useCart.ts
:
src/hooks/useProducts.ts
:
Setting Up Navigation
Create the navigation structure atsrc/navigation/AppNavigator.tsx
:
Creating Example Screens
Create a basic home screen atsrc/screens/HomeScreen.tsx
:
src/screens/CartScreen.tsx
:
Updating App.tsx
Update your rootApp.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 properkeyExtractor
- Implement
getItemLayout
for fixed-height items - Use
maxToRenderPerBatch
andwindowSize
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:- Implement full product detail screens
- Complete cart functionality (update quantities, remove items)
- Add checkout flow
- Implement user authentication with JWT
- Add push notifications for order updates
- Implement offline support with AsyncStorage
- Add analytics and crash reporting