E-Commerce App with React Native and Expo · Course book
Chapter 1: A Shop App from Screens to Server
An online shop is one of the best projects for learning mobile development, because it touches almost every skill an app needs: attractive screens that make products easy to browse, navigation between them, state that has to stay consistent as a cart fills up, sign-in so that orders belong to someone, and a server that holds the real data. This chapter walks through those ideas in the order a shop app is built with React Native and Expo. It explains what Expo adds to React Native, how the main screens are planned, how cart state and prices should be handled so that totals are always correct, how authentication and a backend API fit together, and how automated code review helps keep a growing project tidy. A worked example and a graph show how a free-shipping threshold changes what a customer pays.
Expo, React Native and the project plan
React Native lets you write a mobile app in JavaScript or TypeScript using React components, and renders those components as genuine native interface elements on Android and iOS. Instead of HTML tags such as div and img, you use components such as View, Text, Image, Pressable and FlatList. One codebase produces both apps, which is the main reason small teams choose it.
Expo is a framework and set of tools built around React Native. It creates a working project with one command, provides ready-made modules for things like secure storage, images and fonts, and lets you run the app on your own phone by scanning a code with the Expo Go app while you develop. Expo Router adds file-based routing: each file inside the app folder becomes a screen, folders become groups of screens, and a special layout file can wrap a group in a tab bar or a stack. Adding a screen is therefore as simple as adding a file, and the folder structure doubles as a map of the app.
Before writing code, it pays to list the screens and decide what each one is responsible for. A shop needs fewer screens than you might expect, but each must do its one job well.
| Screen | Main job | Data it needs |
|---|---|---|
| Home | Show featured items and categories, invite browsing | A short list of highlighted products |
| Shop | List every product, with search and filters | The full catalogue, page by page |
| Product | Show photos, price, sizes and an add-to-cart button | One product by its id |
| Cart | Review items, change quantities, see the total | The cart held in app state |
| Profile | Show the signed-in user, past orders and sign-out | The user from the auth provider, orders from the API |
Building the screens
Good shop screens share a few habits. Product images are the heart of the design, so they get generous space and a consistent aspect ratio, which keeps a grid of cards tidy even when the photos differ. Prices sit in a clear, bold style close to the product name. Touch targets are large enough for a thumb, roughly 44 to 48 points, and the most important action on each screen, such as "Add to cart", is visually stronger than everything else.
Lists of products should use FlatList rather than mapping an array inside a scrolling view. A FlatList only renders the items near the visible area and recycles them as the user scrolls, so a catalogue of hundreds of products stays smooth on an older phone. Setting numColumns to 2 produces the familiar two-column product grid, and a stable keyExtractor based on the product id lets React update the list efficiently.
The product screen is reached by tapping a card. With Expo Router, a file named with square brackets, such as product/[id].tsx, becomes a dynamic route: tapping a card navigates to /product/42, and the screen reads the id from the route parameters and loads that product. The same pattern serves every product without a separate screen for each. A bottom tab bar holds the destinations users visit constantly, Home, Shop, Cart and Profile, and a small badge on the cart tab showing the number of items gives reassuring feedback every time something is added.
Styling in React Native uses JavaScript objects created with StyleSheet.create, with layout handled by Flexbox. Defining colours, spacing and font sizes once, in a small theme file, and reusing them everywhere is what makes an app look designed rather than assembled: every card shares the same corner radius, every heading the same weight, and changing the brand colour later means editing one line.
State: products, cart and money
The cart is shared by several screens. The product screen adds to it, the tab bar shows its count, and the cart screen edits it. Data needed in many places belongs in shared state, typically a React context or a small state library, rather than being passed down through every component. Each cart line stores the product id, the chosen size or variant and the quantity. Storing the id rather than a full copy of the product means a price change on the server cannot leave stale prices hidden in the cart; the totals are recalculated from current product data. Keeping the cart in device storage as well means it survives the app being closed.
Money needs particular care. Computers store decimal fractions such as 0.1 in binary, which cannot represent them exactly, so adding 0.1 and 0.2 in JavaScript gives 0.30000000000000004. Small errors like this can make a displayed total disagree with the server by a cent. The standard fix is to store and add prices as whole numbers of the smallest currency unit, cents, and only convert to a decimal string for display.
Thresholds like this are deliberate design choices: they encourage slightly larger orders. The graph below, ignoring tax for simplicity, shows the jump that a threshold creates in the amount paid.
Accounts with Clerk and a backend API
Orders, addresses and order history must belong to a person, so the app needs authentication. Building secure sign-in from scratch is difficult, so many projects use a hosted provider. Clerk supplies sign-up and sign-in flows, email verification and social logins, and its Expo library keeps the user's session token in the phone's secure storage. Screens can then ask whether a user is signed in and show either their profile or a sign-in prompt, and checkout can require an account while browsing stays open to everyone.
The catalogue and orders live on a backend server, here a Node.js application using the Express framework, connected to a database. The server exposes an API: a set of web addresses, called endpoints, that the app calls to read or change data. A shop needs endpoints such as "list products", "get one product", "create an order" and "list my orders". The server, not the app, is the source of truth for prices and stock, because anything running on a customer's phone can be modified. When an order arrives, the server recalculates the total from its own prices and rejects the request if anything does not add up.
Linking the two halves is the final step. Every request from a signed-in user carries their session token, and the server verifies that token before trusting the request, so one customer can never read another's orders. Loading and error states matter here too: while the product list loads, a skeleton or spinner keeps the screen from looking broken, and if the network fails, a clear message and a retry button are far better than an empty page.
Reviewing and shipping the code
As a project grows past a few screens, mistakes creep in: a forgotten loading state, an unhandled error, a component that re-renders far more than necessary. Code review, where someone reads each change before it is merged, catches many of these. AI review tools such as CodeRabbit can comment automatically on each pull request, pointing out likely bugs, inconsistent naming or missing checks. They are useful as a first pass, but they can be wrong or miss context, so their suggestions are weighed like any other reviewer's rather than accepted blindly.
Before release, test on both platforms and on a small phone, because layouts that look fine on a large screen often crowd on a narrow one. Check the empty cart, a failed payment, a slow connection and a signed-out user trying to check out. Expo's build service can then produce the installable Android and iOS packages for the app stores.
Practice tasks
- A cart holds three items at $12.49 and one at $45.00. Work out the subtotal in cents and in dollars.
- Using the same cart, 8% tax and $5 shipping under $100, calculate the total the customer pays.
- Explain why the cart stores product ids rather than copies of each product.
- Why must the server, not the app, recalculate an order's total?
- Name the file you would create with Expo Router for a screen showing one order by its id.
- List three situations you would test before releasing the app, and what the user should see in each.
This course book is available to read online only at chrysolites.org.