Chrysolites Open Courses

E-Commerce App with React Native and Expo · Course book

Chapter 1: A Shop App from Screens to Server

Pairs with Lesson 1: Building 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.

ScreenMain jobData it needs
HomeShow featured items and categories, invite browsingA short list of highlighted products
ShopList every product, with search and filtersThe full catalogue, page by page
ProductShow photos, price, sizes and an add-to-cart buttonOne product by its id
CartReview items, change quantities, see the totalThe cart held in app state
ProfileShow the signed-in user, past orders and sign-outThe 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.

Worked example. A cart holds two T-shirts at $24.99 and one hoodie at $39.50. In cents, the subtotal is 2 × 2,499 + 3,950 = 4,998 + 3,950 = 8,948 cents, which is $89.48. Sales tax of 8% is 8,948 × 0.08 = 715.84 cents, rounded to 716 cents, or $7.16. The shop charges $5.00 shipping on orders under $100. The total is 8,948 + 716 + 500 = 10,164 cents, or $101.64. If the customer adds a $10.52 pair of socks, the subtotal becomes 8,948 + 1,052 = 10,000 cents, exactly $100.00, so shipping becomes free: tax is 800 cents and the total is 10,000 + 800 = 10,800 cents, or $108.00. The extra item costs the customer $6.36 more in total, not $10.52 more, because $5.00 of shipping disappears and tax rises by only $0.84.

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.

subtotal + $5 shipping free shipping subtotal only $0$50$100$150 $0$50$100$150 cart subtotalamount paid before tax
Figure 1.1 — A free-shipping threshold at $100. Just below it, a customer pays $104.99; at exactly $100.00 they pay $100.00, which is why shops display "spend $X more for free shipping" in the cart.

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.

Mobile appExpo + React Native Clerksign-in, sessions Express APINode.js server Databaseproducts, orders 1 sign in2 request + token3 verify token4 read/write
Figure 1.2 — How the pieces talk. The app never touches the database directly; every request passes through the API, which checks who is asking.

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

  1. A cart holds three items at $12.49 and one at $45.00. Work out the subtotal in cents and in dollars.
  2. Using the same cart, 8% tax and $5 shipping under $100, calculate the total the customer pays.
  3. Explain why the cart stores product ids rather than copies of each product.
  4. Why must the server, not the app, recalculate an order's total?
  5. Name the file you would create with Expo Router for a screen showing one order by its id.
  6. List three situations you would test before releasing the app, and what the user should see in each.

Install Chrysolites Open Courses

Android / Chrome / Edge: open the browser menu and choose Install app or Add to Home screen.

iPhone / iPad (Safari): tap the Share button, then Add to Home Screen.