Mastering Expo Router: The Future of File-Based Navigation in React Native π
Learn how the latest version of Expo Router simplifies navigation with file-based routing in React Native β complete with examples & best practices!
The Ultimate Guide to Expo Router (Latest Version)
π Introduction: Why Expo Router?
Navigation in React Native has always been a challenge. Complex navigation stacks, deep linking issues, and manual configurations made it frustrating. But with the latest Expo Router, inspired by Next.js-style file-based routing, things just got WAY EASIER!
π‘ Whatβs new in Expo Router?
βοΈ File-based routing (like Next.js!)
βοΈ Automatic deep linking & dynamic routes
βοΈ No more navigation boilerplate!
βοΈ Works seamlessly with Expo & React NativeIn this guide, weβll explore how Expo Routerβs latest version works and how you can implement it in your React Native apps with examples.
π Getting Started: Installing Expo Router
First, make sure you have the latest Expo SDK installed. If not, install it:
npm install -g expo-cli
Now, create a new project and install the Expo Router package:
npx create-expo-app my-app
cd my-app
npm install expo-router
After installation, add the router provider in your app/_layout.tsx
file:
import { Stack } from "expo-router";
export default function Layout() {
return <Stack />;
}
Thatβs it! Youβre now ready to start building file-based navigation. π
π Understanding File-Based Routing in Expo Router
Expo Router follows a folder-based navigation system just like Next.js.
π Project Structure:
π¦ my-app
β£ π app
β β£ π index.tsx --> Home Screen
β β£ π about.tsx --> About Screen
β β£ π dashboard
β β β£ π index.tsx --> Dashboard Home
β β β£ π settings.tsx --> Dashboard Settings
β β£ π _layout.tsx --> Layout Wrapper
β£ π package.json
π Example: Creating Screens
βοΈ Home Screen (app/index.tsx
)
import { View, Text } from "react-native";
export default function HomeScreen() {
return (
<View>
<Text>Welcome to Expo Router Navigation!</Text>
</View>
);
}
βοΈ About Screen (app/about.tsx
)
import { View, Text } from "react-native";
export default function AboutScreen() {
return (
<View>
<Text>About Page</Text>
</View>
);
}
π Navigation automatically works! No need to set up react-navigation
manually!
π Dynamic Routes & Parameters in Expo Router
Want to handle dynamic URLs like /profile/:id
? Just create a file with square brackets inside your app
folder!
βοΈ Create app/profile/[id].tsx
import { useLocalSearchParams } from "expo-router";
import { View, Text } from "react-native";
export default function ProfileScreen() {
const { id } = useLocalSearchParams(); return (
<View>
<Text>Profile ID: {id}</Text>
</View>
);
}
Now, when you visit /profile/123
, it will show:
Profile ID: 123
π This makes dynamic routing effortless!
π Nested Navigation & Layouts
Expo Router allows nested navigation easily using _layout.tsx
.
βοΈ Example:
π app/dashboard
π dashboard
β£ π _layout.tsx --> Layout for dashboard pages
β£ π index.tsx --> Dashboard Home
β£ π settings.tsx --> Dashboard Settings
βοΈ app/dashboard/_layout.tsx
(Shared Layout)
import { Stack } from "expo-router";
export default function DashboardLayout() {
return (
<Stack>
<Stack.Screen name="index" options={{ title: "Dashboard" }} />
<Stack.Screen name="settings" options={{ title: "Settings" }} />
</Stack>
);
}
Now, all pages inside /dashboard
follow this structure!
π Deep Linking & Authentication with Expo Router
Deep Linking
Expo Router automatically supports deep linking without extra setup!
exp://exp.host/@your-username/my-app/dashboard/settings
Authentication & Protected Routes
Want to protect a page? Add middleware in _layout.tsx
.
import { Redirect } from "expo-router";
export default function Layout() {
const isAuthenticated = false; // Example authentication check if (!isAuthenticated) {
return <Redirect href="/login" />;
} return <Stack />;
}
β
Users who arenβt logged in will be redirected to /login
.
π Final Thoughts: Why Expo Router is the Future?
π Expo Router makes navigation effortless.
πΉ No manual navigation config
πΉ Fully supports deep linking & authentication
πΉ Works out-of-the-box with React Native
π Want to learn more? Check out the official Expo Router docs: https://expo.github.io/router/docs
π Follow me on LinkedIn for more insights!
Auther : Prasad Ranjan
ehttps://www.linkedin.com/in/prasadranjane32