Expo Router

Introduction

Introduction

What is Expo Router?

Introduction

What is Expo Router?

Expo Router vs React Navigation

What is Expo Router?

What is Expo Router?

Expo Router provides file-based routing for applications built with Expo, supporting both React Native and the web

What is Expo Router?

Expo Router provides file-based routing for applications built with Expo, supporting both React Native and the web

It's created to introduce the most effective file-system routing ideas from the web to cross-platform apps on Android, iOS, and the web

Why Expo Router?

Why Expo Router?

When working with React Native, handling navigation can be one of the biggest challenges

Why Expo Router?

When working with React Native, handling navigation can be one of the biggest challenges

React Native developers have traditionally depended on tools like React Navigation, which required manual configuration of navigators, screens, and routes

Why Expo Router?

When working with React Native, handling navigation can be one of the biggest challenges

React Native developers have traditionally depended on tools like React Navigation, which required manual configuration of navigators, screens, and routes

As your application grows, this can quickly become complex and hard to maintain

Why Expo Router?

When working with React Native, handling navigation can be one of the biggest challenges

React Native developers have traditionally depended on tools like React Navigation, which required manual configuration of navigators, screens, and routes

As your application grows, this can quickly become complex and hard to maintain

Expo Router streamlines the whole process by adopting a file-based approach

The way you organise your files and folders directly shapes your app’s navigation structure

Why Expo Router?

The way you organise your files and folders directly shapes your app’s navigation structure

Expo Router apps are designed to cache and run offline-first, updating automatically when a new version is published

Why Expo Router?

The way you organise your files and folders directly shapes your app’s navigation structure

Expo Router apps are designed to cache and run offline-first, updating automatically when a new version is published

Your app can process incoming native URLs without needing a network connection or server

Why Expo Router?

The way you organise your files and folders directly shapes your app’s navigation structure

Expo Router apps are designed to cache and run offline-first, updating automatically when a new version is published

Your app can process incoming native URLs without needing a network connection or server

Expo Router is built on top of React Navigation

Why Expo Router?

The way you organise your files and folders directly shapes your app’s navigation structure

Expo Router apps are designed to cache and run offline-first, updating automatically when a new version is published

Your app can process incoming native URLs without needing a network connection or server

Expo Router is built on top of React Navigation

It's not a full replacement, but an enhancement that makes navigation with more option and easier to manage

Why Expo Router?

Expo Router vs React Navigation

Simplicity

Expo Router vs React Navigation

Simplicity

With React Navigation, you must manually define and set up each navigator, screen, and route, which requires a lot of boilerplate code

Expo Router vs React Navigation

Simplicity

With React Navigation, you must manually define and set up each navigator, screen, and route, which requires a lot of boilerplate code

Expo Router eliminates this by automatically generating routes based on your file structure

Expo Router vs React Navigation

Simplicity

With React Navigation, you must manually define and set up each navigator, screen, and route, which requires a lot of boilerplate code

Expo Router eliminates this by automatically generating routes based on your file structure

Maintainability

Expo Router vs React Navigation

Simplicity

With React Navigation, you must manually define and set up each navigator, screen, and route, which requires a lot of boilerplate code

Expo Router eliminates this by automatically generating routes based on your file structure

Maintainability

As your app expands, managing navigation with React Navigation can become more complicated

Expo Router vs React Navigation

Simplicity

With React Navigation, you must manually define and set up each navigator, screen, and route, which requires a lot of boilerplate code

Expo Router eliminates this by automatically generating routes based on your file structure

Maintainability

As your app expands, managing navigation with React Navigation can become more complicated

With Expo Router, your app’s navigation structure is visually represented by the file system, simplifying understanding and maintenance

Expo Router vs React Navigation

Deep Linking

Expo Router vs React Navigation

Deep Linking

Expo Router automatically sets up deep linking for every screen in your app

Expo Router vs React Navigation

Deep Linking

Expo Router automatically sets up deep linking for every screen in your app

This allows any route to be accessed directly through a URL, which is crucial for features like push notifications, link sharing, and web integration

Expo Router vs React Navigation

Deep Linking

Expo Router automatically sets up deep linking for every screen in your app

This allows any route to be accessed directly through a URL, which is crucial for features like push notifications, link sharing, and web integration

Universal Platform Support

Expo Router vs React Navigation

Deep Linking

Expo Router automatically sets up deep linking for every screen in your app

This allows any route to be accessed directly through a URL, which is crucial for features like push notifications, link sharing, and web integration

Universal Platform Support

Expo Router functions smoothly on Android, iOS, and the web, maintaining the same navigation structure across all platforms

Expo Router vs React Navigation

Deep Linking

Expo Router automatically sets up deep linking for every screen in your app

This allows any route to be accessed directly through a URL, which is crucial for features like push notifications, link sharing, and web integration

Universal Platform Support

Expo Router functions smoothly on Android, iOS, and the web, maintaining the same navigation structure across all platforms

This is especially useful when developing a cross-platform application

Expo Router vs React Navigation

Expo Router vs React Navigation

Deep Linking

Expo Router automatically sets up deep linking for every screen in your app

This allows any route to be accessed directly through a URL, which is crucial for features like push notifications, link sharing, and web integration

Universal Platform Support

Expo Router functions smoothly on Android, iOS, and the web, maintaining the same navigation structure across all platforms

This is especially useful when developing a cross-platform application

Type Safety

Expo Router vs React Navigation

Deep Linking

Expo Router automatically sets up deep linking for every screen in your app

This allows any route to be accessed directly through a URL, which is crucial for features like push notifications, link sharing, and web integration

Universal Platform Support

Expo Router functions smoothly on Android, iOS, and the web, maintaining the same navigation structure across all platforms

This is especially useful when developing a cross-platform application

Type Safety

Expo Router automatically types your routes, ensuring you can only link to valid routes and avoiding errors from linking to non-existent ones

Optimization

In production, routes are automatically optimised with lazy evaluation, while deferred bundling is used in development

Expo Router vs React Navigation

Optimization

In production, routes are automatically optimised with lazy evaluation, while deferred bundling is used in development

This results in faster app loading and more efficient resource usage

Expo Router vs React Navigation

Optimization

In production, routes are automatically optimised with lazy evaluation, while deferred bundling is used in development

This results in faster app loading and more efficient resource usage

Discoverability

Expo Router vs React Navigation

Optimization

In production, routes are automatically optimised with lazy evaluation, while deferred bundling is used in development

This results in faster app loading and more efficient resource usage

Discoverability

Expo Router allows static rendering during build time on the web and connects to native apps.

Expo Router vs React Navigation

Optimization

In production, routes are automatically optimised with lazy evaluation, while deferred bundling is used in development

This results in faster app loading and more efficient resource usage

Discoverability

Expo Router allows static rendering during build time on the web and connects to native apps.

This allows your app's content to be indexed by search engines, making it discoverable - a feature that's typically only available to web apps

Expo Router vs React Navigation

Project Setup

File-based Routing

Expo Router uses a file-system based routing system, meaning the routes available in your app are determined by how you structure your files and folders

File-based Routing

Expo Router uses a file-system based routing system, meaning the routes available in your app are determined by how you structure your files and folders

"Does every file in our app automatically become a route?"

File-based Routing

Expo Router uses a file-system based routing system, meaning the routes available in your app are determined by how you structure your files and folders

"Does every file in our app automatically become a route?"

Expo Router follows a convention-based structure

File-based Routing

  • All routes must be placed inside the app folder.

File-based Routing Conventions

  • All routes must be placed inside the app folder.
  • Any file that exports a React component by default automatically becomes a route.

File-based Routing Conventions

  • All routes must be placed inside the app folder.
  • Each folder repersents a segment of the URL path
  • Any file that exports a React component by default automatically becomes a route.

File-based Routing Conventions

  • All routes must be placed inside the app folder.
  • Each folder repersents a segment of the URL path
  • Any file that exports a React component by default automatically becomes a route.
  • Index files (index.js or index.tsx) represent the root of the segment.

File-based Routing Conventions

By following these conventions, the file is automatically treated as a route in your application

  • All routes must be placed inside the app folder.
  • Each folder repersents a segment of the URL path
  • Any file that exports a React component by default automatically becomes a route.
  • Index files (index.js or index.tsx) represent the root of the segment.

File-based Routing Conventions

Files in the app folder that represent a unique route are called pages

By following these conventions, the file is automatically treated as a route in your application

  • All routes must be placed inside the app folder.
  • Each folder repersents a segment of the URL path
  • Any file that exports a React component by default automatically becomes a route.
  • Index files (index.js or index.tsx) represent the root of the segment.

File-based Routing Conventions

Dynamic Routes

http://localhost:8081/products

Product 1

Product 4

Product 2

Product 3

Product 5

Product 8

Product 6

Product 7

Dynamic Routes

http://localhost:8081/products/1

Product 1

Product 1

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Rs: 440

Add to Cart

Buy Now

Dynamic Routes

Dynamic routes enable the creation of parameterized paths, allowing a single route definition to handle various types of content

Dynamic Routes

Dynamic routes enable the creation of parameterized paths, allowing a single route definition to handle various types of content

Dynamic route segments can be created by using square brackets in the names of files or folders

Dynamic Routes

Dynamic routes enable the creation of parameterized paths, allowing a single route definition to handle various types of content

Dynamic route segments can be created by using square brackets in the names of files or folders

For Example: [param].tsx or [param]/index.tsx

Dynamic Routes

Dynamic routes enable the creation of parameterized paths, allowing a single route definition to handle various types of content

Dynamic route segments can be created by using square brackets in the names of files or folders

For Example: [param].tsx or [param]/index.tsx

The text inside the brackets acts as a parameter that can be accessed
Within our components

Dynamic Routes

Catch-all Segments

http://localhost:8081/products/best-sellers/iphone

Product Details

http://localhost:8081/products/deals/black-friday/iphone

Product Details

Catch-all Segments

http://localhost:8081/products/59883993

Product Details

Catch-all Segments

Catch-all Segments

1. SEO - Search engines. Can the system understand the product's context?

Catch-all Segments

1. SEO - Search engines. Can the system understand the product's context?

2. User experience – URLs represent the navigation path followed.

Catch-all Segments

1. SEO - Search engines. Can the system understand the product's context?

2. User experience – URLs represent the navigation path followed.

3. Analytics – Monitor how users find products.

Dynamic Routes and Multiple Segments

Catch-all Segments

Dynamic Routes and Multiple Segments

- From best sellers: /products/best-sellers/iphone

- From featured deals: /products/deals/black-friday/iphone

- From search results: /products/search/iphone

- From recommendations: /products/recommended/for-you/iphone

- Direct product link: /products/123

Catch-all Segments

Dynamic Routes and Multiple Segments

- From best sellers: /products/best-sellers/iphone

- From featured deals: /products/deals/black-friday/iphone

- From search results: /products/search/iphone

- From recommendations: /products/recommended/for-you/iphone

- Direct product link: /products/123

Two Drawbacks:

Catch-all Segments

Dynamic Routes and Multiple Segments

- From best sellers: /products/best-sellers/iphone

- From featured deals: /products/deals/black-friday/iphone

- From search results: /products/search/iphone

- From recommendations: /products/recommended/for-you/iphone

- Direct product link: /products/123

Two Drawbacks:

Arbitrary depth: Imagine a list of topics stored in a CMS or database, where the depth can vary over time. We can't predict the maximum depth of the navigation tree and create files for each level 

Catch-all Segments

Code Duplication: Whether you're one level deep or ten, the UI used to display the content remains the same. If we rely on multiple levels of dynamic routes, we'll end up with many files and folders containing repetitive code

Two Drawbacks:

Arbitrary depth: Imagine a list of topics stored in a CMS or database, where the depth can vary over time. We can't predict the maximum depth of the navigation tree and create files for each level 

Catch-all Segments

In Expo Router, catch-all segments are defined using the spread syntax inside square brackets: [...params].tsx

Catch-all Segments

In Expo Router, catch-all segments are defined using the spread syntax inside square brackets: [...params].tsx

This special syntax tells Expo Router to match any number of path segments and store them as an array

Catch-all Segments

Not Found Route

Layouts

Layouts

Pages are UI components that are specific to individual routes

Pages are UI components that are specific to individual routes

Layouts are UI components that wrap around pages, letting you share common elements across different routes

Layouts

Pages are UI components that are specific to individual routes

Layouts are UI components that wrap around pages, letting you share common elements across different routes

Think of headers, footers, nav bars, or any UI elements that should stay consistent as users move through your app

Layouts

Pages are UI components that are specific to individual routes

Layouts are UI components that wrap around pages, letting you share common elements across different routes

Think of headers, footers, nav bars, or any UI elements that should stay consistent as users move through your app

To create a layout, default export a React component from a _layout.js or _layout.tsx file

Layouts

Pages are UI components that are specific to individual routes

Layouts are UI components that wrap around pages, letting you share common elements across different routes

Think of headers, footers, nav bars, or any UI elements that should stay consistent as users move through your app

To create a layout, default export a React component from a _layout.js or _layout.tsx file

The layout component renders the Slot component from Expo Router, which gets filled with your page content

Layouts

Route Groups

Route Groups

Route Groups in Expo Router let you organize your routes logically without affecting the URL structure

Route Groups

Route Groups in Expo Router let you organize your routes logically without affecting the URL structure

You may want to organise related routes into a group

Route Groups

Route Groups in Expo Router let you organize your routes logically without affecting the URL structure

You may want to organise related routes into a group

You need to share layouts between specific routes

Route Groups

Route Groups in Expo Router let you organize your routes logically without affecting the URL structure

You may want to organise related routes into a group

You need to share layouts between specific routes

You may want to define multiple root layouts within your app

Stack Navigation

Stack Navigation

Imagine Stack Navigation as a deck of cards

Stack Navigation

Imagine Stack Navigation as a deck of cards

Every new screen you navigate to is like adding a new card on top of the stack

Stack Navigation

Imagine Stack Navigation as a deck of cards

Every new screen you navigate to is like adding a new card on top of the stack

When you go back, it's like removing the top card to reveal the one underneath

Stack Navigation

Imagine Stack Navigation as a deck of cards

Every new screen you navigate to is like adding a new card on top of the stack

When you go back, it's like removing the top card to reveal the one underneath

For example, when browsing a list of products, you tap one to view its details, then tap on a related product to see more information

Stack Navigation

Imagine Stack Navigation as a deck of cards

Every new screen you navigate to is like adding a new card on top of the stack

When you go back, it's like removing the top card to reveal the one underneath

For example, when browsing a list of products, you tap one to view its details, then tap on a related product to see more information

Expo Router simplifies the Stack Navigation implementation with its
built-in Stack component

Tab Navigation

Tab Navigation

Tab Navigation lets you move between different screens by tapping on a tab at the bottom of the screen

Tab Navigation

Tab Navigation lets you move between different screens by tapping on a tab at the bottom of the screen

If you open Reddit, Instagram, Spotify, or Twitter on your phone, you'll see a row of tabs at the bottom

Drawer Navigation

Drawer Navigation

If you’ve used apps like Gmail or Spotify, you’ve seen this before

Drawer Navigation

If you’ve used apps like Gmail or Spotify, you’ve seen this before

When you tap the three-line (hamburger) icon or swipe from the edge, the menu comes in from the side

Modals

Modals

A modal is a screen that pops up over the app to show important info or ask you to choose something

Modals

A modal is a screen that pops up over the app to show important info or ask you to choose something

You can make modals in two different ways:

Modals

A modal is a screen that pops up over the app to show important info or ask you to choose something

You can make modals in two different ways:

- One way is to use the ‘Modal’ component in React Native

Modals

A modal is a screen that pops up over the app to show important info or ask you to choose something

You can make modals in two different ways:

- One way is to use the ‘Modal’ component in React Native

- Another way is to use Expo Router’s file-based setup.

Platform-specific Code

Platform-specific Code

Expo Router uses files to handle navigation in React Native apps

Platform-specific Code

Expo Router uses files to handle navigation in React Native apps

Your app works on Android, iOS, and the web with just one codebase

Platform-specific Code

Expo Router uses files to handle navigation in React Native apps

Your app works on Android, iOS, and the web with just one codebase

Each platform has its own little differences:

Platform-specific Code

Expo Router uses files to handle navigation in React Native apps

Your app works on Android, iOS, and the web with just one codebase

Each platform has its own little differences:

- iOS loves its sleek navigation stacks

Platform-specific Code

Expo Router uses files to handle navigation in React Native apps

Your app works on Android, iOS, and the web with just one codebase

Each platform has its own little differences:

- iOS loves its sleek navigation stacks

- Android has a material design style

Platform-specific Code

Expo Router uses files to handle navigation in React Native apps

Your app works on Android, iOS, and the web with just one codebase

Each platform has its own little differences:

- iOS loves its sleek navigation stacks

- Android has a material design style

- The web focuses on custom layouts and URLs

Platform-specific Code

Expo Router uses files to handle navigation in React Native apps

Your app works on Android, iOS, and the web with just one codebase

Each platform has its own little differences:

- iOS loves its sleek navigation stacks

- Android has a material design style

- The web focuses on custom layouts and URLs

Platform-specific code lets you adjust your app’s behaviour or design for each platform, while keeping the same code structure

API Routes

API Routes

Page routes display UI components

API Routes

Page routes display UI components

API Routes let you create RESTful endpoints and fully control the response

API Routes

Page routes display UI components

API Routes let you create RESTful endpoints and fully control the response

It’s similar to creating a Node + Express app

API Routes

Page routes display UI components

API Routes let you create RESTful endpoints and fully control the response

It’s similar to creating a Node + Express app

You don’t have to set up a separate server

API Routes

Page routes display UI components

API Routes let you create RESTful endpoints and fully control the response

It’s similar to creating a Node + Express app

You don’t have to set up a separate server

API Routes are also useful for making external API requests

API Routes

Page routes display UI components

API Routes let you create RESTful endpoints and fully control the response

It’s similar to creating a Node + Express app

You don’t have to set up a separate server

API Routes are also useful for making external API requests

Since they run on the server, sensitive info like private keys stays safe and doesn’t reach the browser

Next.js 15

By AZ Bytes

Next.js 15

  • 16