Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • jQuery
  • AngularJS
  • ReactJS
  • Next.js
  • React Native
  • NodeJS
  • Express.js
  • MongoDB
  • MERN Stack
  • PHP
  • WordPress
  • Bootstrap
  • Tailwind
  • CSS Frameworks
  • JS Frameworks
  • Web Development
Open In App
Next Article:
How to Create a Protected Route with react-router-dom?
Next article icon

How to Integrate React Router with Remix?

Last Updated : 14 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Integrating React Router with Remix includes the usage of Remix's integrated router abilities thinking that Remix already uses React Router as its foundation. However, if you want to go beyond number one routing and leverage several React Router's extra precise or advanced skills.

You may integrate them smoothly inside your Remix app. Remix uses React Router internally for routing and navigation. This makes it smooth to use React Router's additives (e.g., Link, NavLink, useNavigate, and plenty of others.) alongside Remix’s statistics-loading tool.

You can put in force page-based totally routing with nested layouts, programmatic navigation, and more the usage of React Router in Remix.

These are the following approaches:

Table of Content

  • Basic Navigation Using and
  • Programmatic Navigation with useNavigate
  • Custom Route Matching with useMatch
  • Route Protection and Guards

Steps to Create a Remix Application with React Router

Step 1: Create a New Remix App

This command makes use of npx to run the create-remix bundle, which scaffolds a brand new Remix software. The @ultra-modern flag ensures that you are the use of the latest model.

npx create-remix@latest

Step 2: Navigate to Your Project Directory

This command changes the current listing to the newly created Remix app folder.

cd my-remix-app

Step 3: Install Dependencies

This command installs react-router-dom, which is the library that gives routing abilties for React packages.

npm install react-router-dom

Step 4: Start the Development Server

This command starts offevolved the Remix improvement server, allowing you to view your utility in the browser. The default URL is generally http://localhost:5000.

npm run dev

Project Structure:

Screenshot-2024-09-29-084405

Basic Navigation Using <Link> and <NavLink>

This technique makes use of React Router’s <Link> and <NavLink> additives to navigate between routes in a Remix app. These components provide anchor tag-like navigation with out reloading the net page. In this technique, you make use of Remix's routing capabilities for the primary software flow even as the usage of React Router for precise sections of the app.

Syntax:

<Link to="/path">Link Text</Link>
<NavLink to="/path" activeClassName="active">NavLink Text</NavLink>

Create Routes: Add the subsequent code in app/routes/index.Tsx and app/routes/approximately.Tsx.

  • Visiting http://localhost:3000 shows the Home Page with a hyperlink to the About Page.
  • Clicking the link navigates to the About Page\

Example: This example shows the use of react router with remix.

JavaScript
// app/routes/index.tsx import { Link } from "@remix-run/react";  export default function Index() {   return (     <div>       <h1>Welcome to the Home Page</h1>       <nav>         <ul>           <li><Link to="/about">About Us</Link></li>           <li><Link to="/contact">Contact Us</Link></li>         </ul>       </nav>     </div>   ); } 
JavaScript
// app/routes/about.tsx import { Link } from "@remix-run/react";  export default function About() {   return (     <div>       <h1>About Us</h1>       <p>This is the about page.</p>       <Link to="/">Go Back Home</Link>     </div>   ); } 
JavaScript
// app/routes/contact.tsx import { Link } from "@remix-run/react";  export default function Contact() {   return (     <div>       <h1>Contact Us</h1>       <p>This is the contact page.</p>       <Link to="/">Go Back Home</Link>     </div>   ); } 

Output:

tg

Programmatic Navigation with useNavigate

This approach uses React Router’s useNavigate hook to perform navigation programmatically, based on a few occasion or circumstance on your app. This approach uses React Router to create nested routes within your Remix app. This is beneficial for complex UIs.

Syntax:

const navigate = useNavigate();
navigate('/path');

Create a Dashboard Component: Add the subsequent code in app/routes/dashboard.Tsx

Create Nested Routes: Create a new folder for nested routes inside the routes directory

  • journeying http://localhost:3000/dashboard displays the Dashboard with a nested outlet for the home or settings perspectives.
  • You can navigate to both the home or settings view using URLs like http://localhost:3000/dashboard or http://localhost:3000/dashboard/settings.

Example: This example shows the use of the raect router by programmatic navigation.

JavaScript
// app/routes/about.jsx import { Link } from "@remix-run/react";  export default function About() {   return (     <div>       <h1>About Us</h1>       <p>This is the about page of the website approach 2.</p>       <Link to="/">Go Back Home</Link>     </div>   ); } 
JavaScript
// app/routes/index.jsx import { Link } from "@remix-run/react";  export default function Index() {   return (     <div>       <h1>Welcome to Our Website</h1>       <nav>         <ul>           <li>             <Link to="/about">About Us</Link>           </li>           <li>             <Link to="/contact">Contact Us</Link>           </li>         </ul>       </nav>     </div>   ); } 
JavaScript
// app/routes/contact.jsx import { Link } from "@remix-run/react";  export default function Contact() {   return (     <div>       <h1>Contact Us</h1>       <p>This is the contact page of the website by approach 2.</p>       <Link to="/">Go Back Home</Link>     </div>   ); } 

Output:

pp

Custom Route Matching with useMatch

This technique uses the useMatch hook from React Router to check if the modern course suits a specific direction and conditionally render additives primarily based at the fit. In this approach, React Router handles conditional rendering based on consumer authentication or other situations.

Syntax:

const match = useMatch("/path");

Create Routes: Add the subsequent code in app/routes/index.Tsx and app/routes/approximately.Tsx.

  • Visiting http://localhost:3000 shows the Home Page with a hyperlink to the About Page.
  • Clicking the link navigates to the About Page

Example: This example shows the use of react router.

JavaScript
//your-project\app\routes\_index.tsx import { Link } from "@remix-run/react";  export default function Home() {   return (     <div>       <h1>Home Page</h1>       <nav>         <ul>           <li>             <Link to="/about">About</Link> {/* Links to /about */}           </li>           <li>             <Link to="/contact">Contact</Link> {/* Links to /contact */}           </li>         </ul>       </nav>     </div>   ); } 
JavaScript
//app/routes/contact.jsx import { Link } from "@remix-run/react";  export default function Contact() {   return (     <div>       <h1>Contact Page</h1>       <p>Get in touch with us!</p>       <Link to="/">Go back to Home</Link> {/* Links back to Home */}     </div>   ); } 
JavaScript
//app/routes/about.jsx  import { Link } from "@remix-run/react";  export default function About() {   return (     <div>       <h1>About Page</h1>       <p>This is the about page of the website.</p>       <Link to="/">Go back to Home</Link> {/* Links back to Home */}     </div>   ); } 
JavaScript
// app/root.jsx import { Links, LiveReload, Outlet } from "@remix-run/react";  export default function App() {   return (     <html lang="en">       <head>         <title>Remix App</title>       </head>       <body>         <Outlet /> {/* Renders the current route */}         <LiveReload />       </body>     </html>   ); } 

Output:

m

Route Protection and Guards

This approach leverages conditional logic earlier than navigation to guard sure routes. You can use useNavigate to redirect users based totally on authentication or different conditions. H leverages conditional common sense before navigation to protect positive routes.

Syntax:

if (!isAuthenticated) {
navigate("/login");
}

Create Routes: Add the subsequent code in app/routes/index.Tsx and app/routes/approximately.Tsx.

  • Visiting http://localhost:3000 shows the Home Page with a hyperlink to the About Page.
  • Clicking the link navigates to the About Page.

Example: This example shows the Route Protection and Guards.

JavaScript
//mr-project\app\routes\_index.tsx //mr-project\app\routes\index.jsx import { Link } from "@remix-run/react";  export default function Home() {   return (     <div>       <h1>Welcome to the Home Page</h1>       <nav>         <ul>           <li>             <Link to="./routes/users">View Users</Link> {/* Links to /users */}           </li>         </ul>       </nav>     </div>   ); } 
JavaScript
//mr-project\app\routes\user\index.tsx import { Link } from "@remix-run/react";  const users = [   { id: 1, name: "John Doe" },   { id: 2, name: "Jane Smith" },   { id: 3, name: "Sam Johnson" }, ];  export default function Users() {   return (     <div>       <h1>Users List</h1>       <ul>         {users.map(user => (           <li key={user.id}>             <Link to={`/users/${user.id}`}>{user.name}</Link> {/* Links to dynamic route */}           </li>         ))}       </ul>     </div>   ); } 
JavaScript
//mr-project\app\routes\users\$userId.jsx import { useParams } from "@remix-run/react";  const users = {   1: { name: "John Doe", email: "[email protected]" },   2: { name: "Jane Smith", email: "[email protected]" },   3: { name: "Sam Johnson", email: "[email protected]" }, };  export default function UserDetail() {   const { userId } = useParams(); // Extracts userId from the URL   const user = users[userId];    if (!user) {     return <div>User not found!</div>;   }    return (     <div>       <h1>{user.name}'s Profile</h1>       <p>Email: {user.email}</p>       <Link to="/users">Back to Users List</Link> {/* Link back to users list */}     </div>   ); } 

Output:

nm

Conclusion

Remix already has its personal routing device, so the use of React Router is typically pointless. Remix's routing is report-primarily based, which means that you define your routes within the /routes directory. You can use nested routes and layout routes with Remix for complex UI structures, which can be similar to how React Router handles routing. If you are migrating from a React Router-based app, Remix offers a unbroken transition because it shares many similarities with React Router, along with useNavigate and useParams. You should desire Remix's integrated routing gadget to take complete advantage of its server-facet talents, consisting of information loading, moves, and form handling.


Next Article
How to Create a Protected Route with react-router-dom?

B

badcaptaiz412
Improve
Article Tags :
  • Web Technologies
  • Remix

Similar Reads

  • How to Integrate Redux with React Components ?
    Redux is an open-source JavaScript library for managing and centralizing application state. It helps you to write applications that behave consistently and are easy to test and run in different environments. It can also be understood as the predictable state container for the JavaScript app. It is m
    4 min read
  • Migrate to React Router v6
    React Router v6 brings significant changes and improvements over its predecessor, offering a more intuitive API and better performance. Migrating to React Router v6 might seem daunting, but with proper guidance, the transition can be smooth and beneficial for your React applications. Enhancements in
    3 min read
  • How to Create a Protected Route with react-router-dom?
    A protected route in a React application is a route that only authorized users can access. This is useful for securing parts of an application that should not be available to everyone. We will see how we can create a protected route with react-router-dom. Prerequisites:NPM & NodeJSReactJSSteps t
    3 min read
  • How to Use Routing with React Navigation in React Native ?
    Almost every mobile application requires navigating between different screens. React Native provides an elegant and easy-to-use library to add navigation to native applications: react-navigation. It is one of the most popular libraries used for routing and navigating in a React Native application. T
    4 min read
  • Implementing React Router with Redux in React
    This article explores implementing React Router with Redux for enhanced state management and routing in React applications. Table of Content What is React Router and Redux?Approach to implement React Router in ReduxSteps to Setup the React AppWhat is React Router and Redux?React Router facilitates r
    3 min read
  • How to redirect in React with Typescript ?
    Navigating users seamlessly through a React application is a fundamental aspect of creating a smooth and intuitive user experience. In this article, we delve into the world of redirects in React, specifically addressing the use of TypeScript for enhanced type safety and developer productivity. Prere
    2 min read
  • How to integrate React-PDF in ReactJS ?
    React-PDF is a package that helps users to display PDF files in their React app. By using the react-pdf package we can add PDF files in our React app as if they were images. To use the latest version of react-pdf package, our project should use React 16.3 or later. We can integrate react-pdf using t
    2 min read
  • How to handle Nested Routes in React Router ?
    React Router allows us to create a hierarchical structure where child components can be rendered within parent components, resulting in a seamless navigation flow. In this article, we will see how to handle nested Routes in React Router. Approach to handle Nested RoutesTo implement nested routes in
    3 min read
  • How to programmatically navigate using React Router ?
    React Router provides a way to navigate between different parts of an application when certain events occur, such as button clicks or form submissions. Programmatically navigating allows us to change the URL of an application without a full page reload, enabling a smoother user experience. We will d
    3 min read
  • What is NativeRouter in React Router?
    NativeRouter is a routing solution provided by React Router that is specifically designed for React Native applications. It allows developers to implement routing functionality using a declarative API similar to that of React Router DOM, but tailored for mobile environments. This article explores in
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences