Logo

dev-resources.site

for different kinds of informations.

Routing in React vs. Next.js: Extra Work or Easy Wins?

Published at
9/22/2024
Categories
react
nextjs
routing
webdev
Author
iamfaham
Categories
4 categories in total
react
open
nextjs
open
routing
open
webdev
open
Author
8 person written this
iamfaham
open
Routing in React vs. Next.js: Extra Work or Easy Wins?

Routing. The thing that makes single-page apps feel like real websites. Without it, your app would be one glorified, fancy scroll bar. Today, we're diving deep into the world of React routing versus Next.js routing, specifically looking at some shiny new features from Next.js App Router.

Both React and Next.js offer routing solutions, but as you'll soon see, these two approaches differ like day and night. One involves a bit more boilerplate, while the other is all about developer happiness. Let’s break down the good, the bad, and the “why are we still doing this?” moments.


React Routing: More Work, More Control

React, being a library and not a full-fledged framework, gives you all the control—and by that, I mean all the work. If you’re using React Router (the go-to routing library for React apps), be prepared to define everything yourself.

Example: A Simple React Router Setup

// App.tsx
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import Home from './pages/Home';
import About from './pages/About';

function App() {
  return (
    <Router>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/about" element={<About />} />
      </Routes>
    </Router>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

If you’re familiar with this setup, you know that with great power comes great boilerplate. React Router allows you to define your paths, components, and nested routes manually. You’ll need to set up the <Router> component around your app and manage routes and links manually.

What do you get? Flexibility. You can configure everything exactly how you want it. Want dynamic routes? Go ahead. Need nested routing? You got it. Want to feel like you’re writing routing code until the end of time? Absolutely!


Next.js: Routing That Just Works

Next.js, being a framework, takes away much of that boilerplate. It handles routing automatically based on the file structure. Yes, you read that right: no <Router> component, no manually defining paths. Just create a folder structure, and Next.js will handle the rest. You’ll even get built-in optimizations, SSR (Server-Side Rendering), and all the cool stuff.

Example: Next.js App Router

In Next.js, especially with the App Router, routing has been further streamlined. Here’s how you can define routes with minimal code:

// app/page.tsx (This is the homepage route)
export default function Home() {
  return <h1>Welcome to the homepage!</h1>;
}

// app/about/page.tsx (This is the about page route)
export default function About() {
  return <h1>About Us</h1>;
}

Enter fullscreen mode Exit fullscreen mode

That’s it. No router setup required—Next.js takes care of it under the hood. Want to add dynamic routes? Just name your files with square brackets:

// app/products/[id]/page.tsx (Dynamic route)
export default function Product({ params }) {
  return <h1>Product ID: {params.id}</h1>;
}

Enter fullscreen mode Exit fullscreen mode

Key Features of Next.js' App Router

  • File-based routing: The file/folder structure drives the routes. If it's in the app/ directory, it's a route.
  • Dynamic routes: Easy as naming a file [id]/page.tsx.
  • Layouts and templates: With App Router, you can now have layouts and templates that allow for reusable components across different routes.

For example, having a layout.tsx file in any folder will apply that layout to all routes within that folder.

// app/layout.tsx (Shared layout for all pages)
export default function RootLayout({ children }) {
  return (
    <html>
      <body>
        <nav>/* navigation */</nav>
        {children}
      </body>
    </html>
  );
}
Enter fullscreen mode Exit fullscreen mode

Summarizing the Key Differences Between React and Next.js Routing

Now that we’ve seen some code, let’s talk about the differences between these two approaches. While React Router gives you full control, Next.js routing is about developer productivity.

Feature React Router (React) Next.js (App Router)
Router Setup Requires <Router> and manual setup Automatic based on folder structure
Dynamic Routing Manual with <Route> and path parameters Automatic with file naming conventions
Nested Routes Defined manually in JSX Inferred from folder structure
Layouts Manual, not built-in Built-in with layout.tsx files
Data Fetching Handled separately (useEffect, SWR, etc.) Built-in with Next.js API routes or Server Components
SEO Optimization Custom configuration required Automatic SEO and performance enhancements
SSR/SSG Not built-in, requires additional libraries Fully supported out of the box

Which One Should You Use?

  • If you want full control and don’t mind handling every detail, React Router is for you. It’s like cooking from scratch—labor-intensive but rewarding.

  • If you want a routing system that just works out of the box, Next.js is your best friend. Why reinvent the wheel when you can have a Tesla? Especially with Next.js, App Router streamlines development like never before.


Conclusion: The Verdict

So, React Router gives you control, but at what cost? Boilerplate. And let's face it, managing routes manually is so 2015. With Next.js, especially the new App Router, routing is simplified to the point where you can focus more on building your app, not wiring it up.

In other words, Next.js is like hiring someone to do your grocery shopping while you relax—React Router, on the other hand, is you, standing in line with a full cart wondering why you decided to make a ten-course meal from scratch.

The choice is yours, but with Next.js, the scales are tipping heavily towards effortless productivity.


Connect with me on LinkedIn and X

routing Article's
30 articles in total
Favicon
Mastering Nested Navigation in Flutter with `go_router` and a Bottom Nav Bar
Favicon
Understanding ShellRoute in go_router: Managing Shared Layouts Effectively
Favicon
How the Web Evolved: The Rise of Single Page Applications
Favicon
Introducing Humiris MoAI Basic : A New Way to Build Hybrid AI Models
Favicon
TanStack Router: The Future of React Routing in 2025
Favicon
Next.js: Dynamic Routing with API Integration.
Favicon
Learning Next.js 13 App Router: A Comprehensive Guide 🚀
Favicon
Organizing Your Routes Modularly and Automatically in Lithe
Favicon
Organizando Suas Rotas de Forma Modular e Automática no Lithe
Favicon
🗺️ Peta Jalan Laravel: Menjelajah Routing, Middleware, dan Controller (Indonesian Version)
Favicon
Working On a React / Python Flask Back End Web App.
Favicon
Vue.js for Beginners 2024 #VueJs Part 5 : A Complete Guide to Routing with Vue Router
Favicon
Mastering Routing Protocols with Cisco Packet Tracer: A Learning Experience
Favicon
Restful Routing - A Flask API Example
Favicon
Routing in React vs. Next.js: Extra Work or Easy Wins?
Favicon
Browser refresh on click of Home button using href
Favicon
Decorate the Symfony router to add a trailing slash to all URLs
Favicon
Routing in Umbraco Part 2: Content Finders
Favicon
Routing in Umbraco Part 1: URL segments
Favicon
Simplifying Network Administration: BGP Route Servers' Function in the Internet Ecosystem
Favicon
createBrowserRouter A step up from Switch
Favicon
Mastering Angular 17 Routing
Favicon
Snag the Current URL in SvelteKit (2024 Edition)
Favicon
Ensuring Type Safety in Next.js Routing
Favicon
Laravel 11: Health Check Routing Example
Favicon
Switch'in L2 mi L3 mĂĽ OlduÄźunun Ă–Äźrenilmesi
Favicon
Routing with React Router
Favicon
Routing implementation using PHP attributes
Favicon
What is Vinxi, and how does it compare to Vike?
Favicon
Fallback Routing with Axum

Featured ones: