Chrysolites Tech Academy

Ride-Hailing App Clones · Course book

Chapter 5: Booking a Ride on a Phone

Pairs with Lesson 5: A React Native Ride App with Redux and Google Places

The final project in this course builds a complete ride-booking app for iOS and Android with React Native, from the first screen to a priced ride. It brings together three tools that appear in almost every serious mobile app: React Navigation for moving between screens, Redux for state that many screens share, and Google's location services for place search, directions and travel times. This chapter explains how they fit together: the shape of the app and its state, autocomplete search for places, the map with its markers and route, travel time from a distance matrix, a price for each ride option, and the layout details, such as keyboards and safe areas, that make a mobile app comfortable to use.

The app's shape and its shared state

The app has a small number of screens. The Home screen shows the logo, a search box for the pickup location and a row of options, such as booking a ride or ordering food. Choosing a ride opens the Map screen, whose top half is a map and whose bottom half holds a smaller stack of cards: a Navigate card asking "Where to?", and then a Ride options card listing the available rides with their prices.

Several pieces of information are needed by more than one screen: where the trip starts, where it ends and how long it will take. Passing them from component to component as props would be clumsy, so they live in a Redux store, created with Redux Toolkit.

const navSlice = createSlice({
  name: "nav",
  initialState: { origin: null, destination: null, travelTimeInformation: null },
  reducers: {
    setOrigin: (state, action) => { state.origin = action.payload; },
    setDestination: (state, action) => { state.destination = action.payload; },
    setTravelTimeInformation: (state, action) => { state.travelTimeInformation = action.payload; },
  },
});

export const selectOrigin = (state) => state.nav.origin;

Components read values with useSelector(selectOrigin) and change them with useDispatch, for example dispatch(setOrigin({ location, description })). Redux Toolkit lets reducers appear to modify state directly, but behind the scenes it produces a new immutable state, which is how React knows to re-render. Writing selectors for each value keeps components independent of the exact shape of the store. The project is written in TypeScript, so the slice can also declare the types of its state, catching mistakes such as treating a missing destination as present.

Navigation: stacks within stacks

React Navigation manages screens as stacks. The top-level stack holds the Home and Map screens; pressing a button calls navigation.navigate("MapScreen"), and the back gesture returns. Inside the Map screen, a second, nested stack switches between the Navigate card and the Ride options card in the bottom half while the map stays in place. Hiding the default headers gives full control over the design, with a custom menu button floating over the map to return home.

Screen or cardReads from the storeWrites to the store
Home screenorigin, to enable the ride optionorigin, from the pickup search
Navigate cardnothing essentialdestination, from the "Where to?" search
Maporigin and destinationtravelTimeInformation, from the distance matrix
Ride options cardtravelTimeInformationnothing; it only displays prices

The ride button on the Home screen is disabled, and shown faded, until an origin has been chosen, a small guard that prevents the Map screen from opening with nothing to show.

Searching for places with autocomplete

Typing a full address on a phone is slow and error-prone. A places autocomplete component calls Google's Places service as the rider types and shows a list of suggestions; choosing one returns the place's description and, with the details option enabled, its coordinates. The component is configured with the API key, the language, a minimum number of characters before searching, and a debounce delay of around 400 milliseconds, so that a request is sent only after the rider pauses rather than on every keystroke. For a ten-letter search typed steadily, that means one or two requests instead of ten.

When a suggestion is chosen, the component's callback dispatches the location to the store, as origin on the Home screen or destination on the Navigate card, and the destination choice then navigates to the Ride options card. A favourites list, such as Home and Work with saved addresses, offers one-tap destinations.

The autocomplete component needs some care to behave well. It should not fetch suggestions for one or two letters, which match far too many places; a minimum of around two or three characters is common. Results can be biased towards the rider's area or restricted to one country, so that typing "High Street" finds the nearby one first. The list of suggestions must sit above other content, which on some platforms means giving its container a higher stacking order and making sure taps on a suggestion are not swallowed by the keyboard closing. Finally, every request to a paid service has a cost, so debouncing, minimum lengths and session tokens, which group a series of keystrokes and the final selection into one billable session, all help keep the bill small.

The map, the route and the travel time

The map reads origin and destination from the store. It places a marker at each, and draws the driving route between them with a directions component that calls Google's Directions service and renders the result as a line. When both markers exist, the map calls fitToSuppliedMarkers with some edge padding so the whole trip is visible. Because markers are added after the map renders, this zoom is triggered in an effect that runs when origin or destination changes, after a short delay so the markers are in place.

The route line shows the path, but the price needs numbers. The Distance Matrix service returns, for each origin and destination, a distance and a duration, each with a readable text such as "21.3 km" or "27 mins" and a numeric value in metres or seconds. The map's effect fetches it when both points are set and dispatches the result to the store, where the Ride options card reads it. Requesting the units explicitly, metric or imperial, makes the distance text match what riders expect where they live, while the numeric values stay in metres and seconds for calculations.

Pricing the ride options

The Ride options card lists a few ride types from a data array, each with an id, a title, an image and a multiplier. One simple, transparent approach prices each ride from the trip's duration: the duration in seconds times a surge rate times the ride's multiplier, divided by 100, giving an amount in the local currency. Using duration rather than distance means slow traffic costs more, which reflects the driver's time.

Worked example. The distance matrix reports a duration of 1,612 seconds, and the surge rate is 1.5. A standard ride, multiplier 1, costs 1,612 × 1.5 × 1 / 100 = 24.18. A larger car, multiplier 1.2, costs 24.18 × 1.2 = 29.02. A luxury ride, multiplier 1.75, costs 24.18 × 1.75 = 42.32. Formatting each with Intl.NumberFormat and a currency code adds the symbol and two decimal places. If the surge rate rose to 2.0 at a busy time, every price would rise by a third, the standard ride to 32.24.
standard 36.00 larger 43.20 luxury 63.00 0204060 010203040 trip duration (minutes)price
Figure 5.1 — Duration-based prices with a surge rate of 1.5. Each minute adds 0.90 to a standard ride, 1.08 to a larger car and 1.575 to a luxury ride.

Selecting a ride highlights its row and enables a "Choose" button at the bottom that names the selection. The card also shows the trip's distance and duration text from the store, so the rider sees why the price is what it is. In a production app the final price would be confirmed by the server at booking time, since any value computed on the phone can be changed by a determined user, but the on-device estimate is what makes the options card feel instant. Showing a loading placeholder until the travel time arrives avoids displaying prices of zero.

Keyboards, safe areas and finishing touches

On phones, the on-screen keyboard can cover the input being typed into. Wrapping the Navigate card in a KeyboardAvoidingView, with padding behaviour on iOS and height behaviour on Android, moves content up when the keyboard appears. Safe area handling keeps buttons clear of notches and home indicators. Styling with a Tailwind-style library for React Native keeps layouts concise, and a few icons from an icon set complete the look. Finally, a run through the whole flow on both iOS and Android, choosing a pickup, a destination and a ride, catches the small platform differences that simulators can hide.

Practice tasks

  1. Using the pricing approach in this chapter with a surge rate of 1.5, price all three rides for a trip of 900 seconds.
  2. A trip of 1,612 seconds is priced with a surge rate of 1.8. What does the larger car cost?
  3. Write the reducer and action you would add to clear the destination when the rider goes back to the Home screen.
  4. Explain why the ride button on the Home screen is disabled until an origin is chosen.
  5. How many autocomplete requests would a 12-letter search make with no debounce, and roughly how many with a 400-millisecond debounce if the rider pauses once?
  6. Describe what KeyboardAvoidingView does and why its behaviour differs between iOS and Android.

Install Chrysolites Tech Academy

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.