Chrysolites Tech Academy

Short-Video App with React Native and Supabase · Course book

Chapter 3: Styles and Routes

Pairs with Lesson 3: Styling with NativeWind and Routing with Expo Router

With a clean Expo project running, two decisions shape everything that follows: how screens are styled, and how the user moves between them. This chapter adds NativeWind, which brings Tailwind CSS's utility classes to React Native, and builds out Expo Router, which turns the folder structure into the app's navigation. It explains how utility-class styling works and how NativeWind is configured, how Tailwind's spacing and type scales translate into sizes on screen, worked by hand, how Expo Router maps files to screens with layouts, groups and dynamic routes, how the authentication screens are separated from the tabs, and how to debug the routing mistakes that are most common when a file structure grows.

Utility-class styling with NativeWind

React Native styles are JavaScript objects: { flex: 1, padding: 16, backgroundColor: "black" }. They work well, but long style objects for every element become verbose, and keeping spacing and colours consistent across many screens takes discipline. Tailwind CSS takes a different approach on the web: instead of writing styles, you compose small, single-purpose classes such as flex-1, p-4 and bg-black. Because every value comes from a shared scale, designs stay consistent almost automatically.

NativeWind makes the same classes work in React Native. You write <View className="flex-1 items-center justify-center bg-black">, and NativeWind converts the classes into React Native style objects. It supports responsive and state variants too, such as dark: for dark mode, and active: for styles applied while a pressable element is pressed.

Setting it up involves a few configuration files, each with a job.

FileWhat to addWhy
tailwind.config.jsThe content paths, such as ./app/**/*.{js,jsx,ts,tsx} and ./components/**/*.{js,jsx,ts,tsx}, and the NativeWind presetTailwind generates styles only for classes found in these files
global.cssThe three Tailwind directives for base, components and utilitiesThe entry point Tailwind processes
Babel and Metro configurationNativeWind's Babel preset and Metro wrapperTransforms className and includes the CSS in the bundle
Root layoutAn import of global.cssLoads the styles once for the whole app
A TypeScript declaration fileNativeWind's type referenceLets TypeScript accept the className prop

The content paths are the most common source of trouble. If a folder is missing from them, classes used there are silently ignored, and elements appear unstyled. After changing the configuration, restart the development server with the cache cleared so the new settings take effect.

Scales, sizes and custom styles

Tailwind's sizes come from scales rather than arbitrary numbers. In the spacing scale, used by padding, margin, width, height and gaps, each step is a quarter of a rem, the root font size. On the web a rem is 16 pixels by default; NativeWind lets you choose the value used on native platforms, so check your configuration if sizes look different from your design.

Worked example. With a 16-pixel rem, p-4 is 4 × 0.25 rem = 1 rem = 16 pixels of padding on every side, and p-6 is 1.5 rem = 24 pixels. A square avatar of w-12 h-12 is 12 × 0.25 = 3 rem = 48 pixels each way, a good size for a tap target. In the type scale, text-sm is 0.875 rem = 14 pixels, text-base is 1 rem = 16 pixels and text-2xl is 1.5 rem = 24 pixels. A card with p-4 inside a screen with px-4 on a 375-pixel phone leaves 375 − 2 × 16 − 2 × 16 = 311 pixels of width for its content.

When the scale does not have the value you need, Tailwind accepts arbitrary values in square brackets, such as w-[72px] or bg-[#fe2c55], though relying on them heavily undermines consistency. A better approach for brand colours is to extend the theme in the configuration, adding named colours such as primary that can then be used as bg-primary or text-primary everywhere.

Reusable components keep repeated combinations in one place. A button component, for example, can accept a title and an onPress handler and apply the same classes for padding, rounded corners and colour every time, perhaps with a variant prop for primary and secondary styles. If the design changes, one file changes. Components should still accept a className prop so that a particular screen can add spacing around them.

Not every web habit carries over. React Native has no cascading styles, so text colour and size must be set on the <Text> element itself, not on a surrounding <View>. Flexbox is the only layout system, and its default direction is a column rather than a row, so flex-row is needed far more often than on the web. Shadows behave differently on iOS and Android, and some properties, such as certain hover effects, have no meaning on a touch screen. A short-video app's video screens use a black background throughout, so setting the colour scheme deliberately, rather than letting each screen follow the system's light or dark mode, keeps the experience consistent.

Expo Router: files become screens

Expo Router builds navigation from the app folder. Each file exports a component that becomes a screen, and its path becomes the route. Special files and folder names add structure.

  • Layouts: a _layout.tsx file wraps every screen in its folder. Returning <Stack /> makes the screens push and pop like a stack of cards; returning <Tabs /> makes them tabs.
  • Groups: a folder in parentheses, such as (tabs) or (auth), groups screens and gives them their own layout without adding to the route.
  • Dynamic routes: a file in square brackets, such as user/[id].tsx, matches any value in that position; the screen reads it with useLocalSearchParams.
  • Index files: index.tsx is the route for its folder itself.

Moving between screens uses the Link component, like a link on the web, or the router object in code: router.push("/comments") adds a screen on top, router.back() returns, and router.replace("/(tabs)") swaps the current screen without leaving it in the history, which suits moving from login to the main app, where the back button should not return to the login screen.

Each layout can set screen options: whether a header is shown, its title, its colours and, for tabs, the label and icon. Options can apply to every screen in a layout or to one screen in particular, such as hiding the header on the full-screen feed while showing a titled header on the comments screen.

Root stack (auth) stack(tabs) tabsscreens on top loginsignupindex (home)friendscamerainboxprofilecommentsuser/[id]chat/[id]
Figure 3.1 — The app's route tree. The root stack holds the auth screens, the tabs and screens that open on top of the tabs, such as comments.

Separating authentication from the tabs

A social app shows different worlds to signed-out and signed-in users. Putting the login and sign-up screens in an (auth) group with its own stack layout keeps them free of the tab bar, while the main experience lives in the (tabs) group. The root layout decides which group to show. In this chapter that decision can be a placeholder; in a later chapter, an authentication provider supplies the real signed-in state, and the root layout redirects to the login screen when no user is present and to the tabs when one is.

A custom login page already shows the shape of what is to come: the app's logo, inputs for email and password, a primary button and a link to the sign-up page. Styling it with NativeWind takes only a handful of classes: a full-screen container with flex-1 justify-center px-6 bg-white, inputs with a border, padding and rounded corners, and a button with a strong background colour and white bold text. Building the page now, even without working sign-in, lets you test the navigation between the auth screens and the tabs.

Debugging routes and structure

Routing mistakes produce a few recognisable symptoms. An "unmatched route" screen means the address does not match any file: check the spelling, the folder and whether a group's parentheses are correct. A screen appearing in the tab bar that should not is usually a file accidentally placed inside the (tabs) folder; screens that should open on top of the tabs belong at the root instead, or can be hidden from the bar in the tab layout's options. A missing or misplaced _layout.tsx means screens fall back to a default layout, so headers or tabs may look wrong. Duplicate routes, for example both home.tsx and home/index.tsx, cause confusing behaviour and should be avoided.

Expo Router also generates types for routes, so with TypeScript, linking to a route that does not exist can be flagged before the app runs. Keeping the folder tree tidy, with groups named for their purpose and stand-alone screens at the root, makes the navigation easy to reason about as the app grows.

Practice tasks

  1. With a 16-pixel rem, convert p-5, w-16, text-lg (1.125 rem) and gap-3 into pixels.
  2. A screen has px-6 and a card inside it has p-3. How wide is the card's content area on a 390-pixel phone?
  3. Write the content paths needed if components live in both components/ and features/.
  4. Explain the difference between router.push and router.replace, and which suits moving from login to the tabs.
  5. Draw the files needed for a dynamic route that shows a user's profile by id, and the hook that reads the id.
  6. A comments screen appears as an extra tab. What is the likely cause, and two ways to fix it?

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.