Routing in React 18 and Beyond

Rate this content
Bookmark

Concurrent React and Server Components are changing the way we think about routing, rendering, and fetching in web applications. Next.js recently shared part of its vision to help developers adopt these new React features and take advantage of the benefits they unlock.

In this talk, we’ll explore the past, present and future of routing in front-end applications and discuss how new features in React and Next.js can help us architect more performant and feature-rich applications.

FAQ

Next.js is a framework developed by Vercell to help developers build server-side rendered applications. It uses file system-based routing, where files in your application map to your URL.

Vercell is a platform created by the same company that developed Next.js. It supports over 35 front-end frameworks, allowing developers to use it even if they don't utilize Next.js.

Hybrid apps combine the benefits of both client and server environments, trying to leverage the advantages of each. This type of application is part of an emerging pattern recognizing the transition between different environments.

React 18 introduces concurrent features that significantly influence how developers build applications. It emphasizes hybrid solutions that minimize server requests while enhancing user experience and performance.

The new routing system in Next.js aims to enable layouts that don't lose state or re-render unnecessarily, allow server-only rendering for changed segments during navigation, and offer more granular control over data fetching.

React server components allow for server-side rendering of components, reducing the overall code sent to the client and improving loading times. Next.js integrates these components to enhance performance and user experience in web applications.

The new Next.js router supports nested routing, enables the creation of persistent layouts, and optimizes data fetching and rendering processes to reduce loading times and server workload.

Delba de Oliveira
Delba de Oliveira
20 min
17 Jun, 2022

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Routing in React 18 brings a native app-like user experience and allows applications to transition between different environments. React Router and Next.js have different approaches to routing, with React Router using component-based routing and Next.js using file system-based routing. React server components provide the primitives to address the disadvantages of multipage applications while maintaining the same user experience. Improving navigation and routing in React involves including loading UI, pre-rendering parts of the screen, and using server components for more performant experiences. Next.js and Remix are moving towards a converging solution by combining component-based routing with file system routing.

1. Introduction to Routing in React 18

Short description:

Hey, everyone, I'm Delba from Vercell. Today, I want to talk about routing in React 18 and how it will change the way developers view applications. Routing has evolved from multi-page applications to single-page applications. Single-page applications bring a native app-like user experience. The client-side decides what content to fetch and render. Applications are transitioning between different environments, client or server.

♪♪ ♪♪ Hey, everyone, I'm Delba, and I work for Vercell. As some of you may know, we're the creators of Next.js, and I'm curious to know how many of you here use Next.js? Wow, so that's a lot of you. That's amazing. So, although we created Next.js, we also have a platform, Vercell. And funnily enough, Vercell supports over, I think, something like 35-plus front-end frameworks. So even if you don't use Next.js, you could still use Vercell.

And today, I want to talk to you about some exciting stuff we have been working on, which is routing. And routing in React 18. Now, as you may know, a few months ago, React 18 was released with new concurrent features. And on the React blog, the team mentioned that they expect concurrent features to have a big impact on the way that developers viewed applications. So today, I want to discuss with you what this impact could mean and how we will change how developers view applications, especially with React Server components, as well. And this may not also change things for Next.js but also for other frameworks and library maintainers. And I'm going to specifically look at routing, but I will also mention data fetching and rendering, or as I like to call them, the three pillars of the web, because those terms are very much interconnected.

Now, to give everyone here some context and also the people who might be watching online, I think it's important to just take a step back and look at how routing has evolved in frontend applications. Now, please note, I'm going to be condensing years of routing history in like five minutes, so there's a lot more nuance. And one way we can look at routing is through the type of applications we can build, multi-page applications, single-page applications and, more recently, hybrid apps. So in the very early web, routing was very straightforward because if you think about it, each URL mapped to a specific file on a server. And then later on, using dynamic server-side languages, we were able to generate a response from the server for a specific route. In a traditional multi-page application, routing is done on the server, and navigating between pages causes the full page reload that we're all very used to. Now, when native mobile apps came along, they brought smoother transitions and new UI patterns. And single-page applications, you could say, were the web's response to native apps. We wanted our websites to feel like native apps. That is to say, we wanted them to have the same user experience as native apps. In a traditional single-page application, routing is done on the client-side. So on initial load, you may see a white blank screen while the client-side fetches and renders the content. Now, when you navigate on a single-page application, the client will dynamically rewrite new content. And for a given route, the client is deciding what content to fetch and render. And last year, Rich Harris, the creator of Svelte, gave an amazing talk on the whole MPA versus SPA debate, where he discussed some of the pros and cons of each. It's a really great talk, and I recommend watching it if you haven't had a chance to do it already. But one takeaway from that talk that I want you to remember is that he recognized that there's an emerging pattern in our industry, where applications are transitioning between different environments, client or server.

2. Overview of Routing in React

Short description:

And he called those type of applications transitional apps. React Router's approach to routing is what I like to call component-based routing. Next.js took a different approach to routing using file system-based routing. The React team gave us an early Christmas present that will allow us to move towards more hybrid solutions.

And he called those type of applications transitional apps. And transitional apps, they try to combine the benefits of both the client and the server, because if you think about it, why not both? So that's kind of like a broad overview of routing on the web. Now let's zoom into React.

While React didn't invent single-page applications, you could argue that they contributed to their popularity. The fact that React was and still is mainly concerned with UI and rendering means that the community has come up with a few different solutions for routing. And one client-side solution that quickly rose to popularity was React Router. Now, how many of you have used React Router before? Yes.

So React Router's approach to routing is what I like to call component-based routing where you use code to map specific components in your application to your URL path. And if you combine it with a tool chain like create React app, then you can easily create single-page applications. And this leads to I think not just the adoption of React itself, but also applications that were fully client-side rendered.

Now, in 2016, a few years later, Vessel introduced Next.js. And Next.js was created as a framework to help developers build server-side rendered applications. And Next.js took a different approach to routing. It used what I like to call file system-based routing, where files in your application map to your URL. And although Next.js felt very similar to a multi-page application, it actually used prefetching and client-side navigation to give applications a Spar-like feel, if you could say.

Now, another incremental step towards hybrid apps was the Next.js data fetching methods, like getInitialProps. And what these data fetching methods did was move the fetching outside of your rendering code or outside of your component so that you could fetch data both from the client and the server. Now, I'm focusing on Next.js today. But it would be remiss of me not to acknowledge some projects that are also working on routing solutions for React, including Shopify's Hydrogen, Remix, and also Redwood. So fast-forward a few years in early 2020, and the members of the React team were publicly discussing moving more rendering work to the server.

The idea was that if we are doing data fetching on the server anyway, could we move some rendering work to the server and, therefore, reduce the amount of code that gets sent to the client? Now, you can probably imagine the hot takes that followed that tweet. I think it can be best summarized as, this looks a lot like server-sided routing. Are we going back to MPAs? But to quote a not so serious meme from one of my favourite people on Twitter, this is less of a pendulum swing and more a spiral of incremental improvement. So switchbacks. Not purely SPA, not purely MPA, but a kind of spike in convergence towards hybrid. That benefits from both the server and the client. And each time this conversation was brought up, the reacting was careful to emphasize that they were looking for a hybrid solution. And one important thing to note here is that this hybrid solution wouldn't be creating additional requests to the server. It would take advantage of a request that has to exist anyways. So, in December 2020, the React team gave us an early Christmas present that will allow us to move towards more hybrid solutions.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

A Guide to React Rendering Behavior
React Advanced Conference 2022React Advanced Conference 2022
25 min
A Guide to React Rendering Behavior
Top Content
React is a library for "rendering" UI from components, but many users find themselves confused about how React rendering actually works. What do terms like "rendering", "reconciliation", "Fibers", and "committing" actually mean? When do renders happen? How does Context affect rendering, and how do libraries like Redux cause updates? In this talk, we'll clear up the confusion and provide a solid foundation for understanding when, why, and how React renders. We'll look at: - What "rendering" actually is - How React queues renders and the standard rendering behavior - How keys and component types are used in rendering - Techniques for optimizing render performance - How context usage affects rendering behavior| - How external libraries tie into React rendering
Building Better Websites with Remix
React Summit Remote Edition 2021React Summit Remote Edition 2021
33 min
Building Better Websites with Remix
Top Content
Remix is a new web framework from the creators of React Router that helps you build better, faster websites through a solid understanding of web fundamentals. Remix takes care of the heavy lifting like server rendering, code splitting, prefetching, and navigation and leaves you with the fun part: building something awesome!
React Compiler - Understanding Idiomatic React (React Forget)
React Advanced Conference 2023React Advanced Conference 2023
33 min
React Compiler - Understanding Idiomatic React (React Forget)
Top Content
React provides a contract to developers- uphold certain rules, and React can efficiently and correctly update the UI. In this talk we'll explore these rules in depth, understanding the reasoning behind them and how they unlock new directions such as automatic memoization. 
Using useEffect Effectively
React Advanced Conference 2022React Advanced Conference 2022
30 min
Using useEffect Effectively
Top Content
Can useEffect affect your codebase negatively? From fetching data to fighting with imperative APIs, side effects are one of the biggest sources of frustration in web app development. And let’s be honest, putting everything in useEffect hooks doesn’t help much. In this talk, we'll demystify the useEffect hook and get a better understanding of when (and when not) to use it, as well as discover how declarative effects can make effect management more maintainable in even the most complex React apps.
(Easier) Interactive Data Visualization in React
React Advanced Conference 2021React Advanced Conference 2021
27 min
(Easier) Interactive Data Visualization in React
Top Content
If you’re building a dashboard, analytics platform, or any web app where you need to give your users insight into their data, you need beautiful, custom, interactive data visualizations in your React app. But building visualizations hand with a low-level library like D3 can be a huge headache, involving lots of wheel-reinventing. In this talk, we’ll see how data viz development can get so much easier thanks to tools like Plot, a high-level dataviz library for quick & easy charting, and Observable, a reactive dataviz prototyping environment, both from the creator of D3. Through live coding examples we’ll explore how React refs let us delegate DOM manipulation for our data visualizations, and how Observable’s embedding functionality lets us easily repurpose community-built visualizations for our own data & use cases. By the end of this talk we’ll know how to get a beautiful, customized, interactive data visualization into our apps with a fraction of the time & effort!
React Concurrency, Explained
React Summit 2023React Summit 2023
23 min
React Concurrency, Explained
Top Content
React 18! Concurrent features! You might’ve already tried the new APIs like useTransition, or you might’ve just heard of them. But do you know how React 18 achieves the performance wins it brings with itself? In this talk, let’s peek under the hood of React 18’s performance features: - How React 18 lowers the time your page stays frozen (aka TBT) - What exactly happens in the main thread when you run useTransition() - What’s the catch with the improvements (there’s no free cake!), and why Vue.js and Preact straight refused to ship anything similar

Workshops on related topic

React Performance Debugging Masterclass
React Summit 2023React Summit 2023
170 min
React Performance Debugging Masterclass
Top Content
Featured WorkshopFree
Ivan Akulov
Ivan Akulov
Ivan’s first attempts at performance debugging were chaotic. He would see a slow interaction, try a random optimization, see that it didn't help, and keep trying other optimizations until he found the right one (or gave up).
Back then, Ivan didn’t know how to use performance devtools well. He would do a recording in Chrome DevTools or React Profiler, poke around it, try clicking random things, and then close it in frustration a few minutes later. Now, Ivan knows exactly where and what to look for. And in this workshop, Ivan will teach you that too.
Here’s how this is going to work. We’ll take a slow app → debug it (using tools like Chrome DevTools, React Profiler, and why-did-you-render) → pinpoint the bottleneck → and then repeat, several times more. We won’t talk about the solutions (in 90% of the cases, it’s just the ol’ regular useMemo() or memo()). But we’ll talk about everything that comes before – and learn how to analyze any React performance problem, step by step.
(Note: This workshop is best suited for engineers who are already familiar with how useMemo() and memo() work – but want to get better at using the performance tools around React. Also, we’ll be covering interaction performance, not load speed, so you won’t hear a word about Lighthouse 🤐)
Concurrent Rendering Adventures in React 18
React Advanced Conference 2021React Advanced Conference 2021
132 min
Concurrent Rendering Adventures in React 18
Top Content
Featured WorkshopFree
Maurice de Beijer
Maurice de Beijer
With the release of React 18 we finally get the long awaited concurrent rendering. But how is that going to affect your application? What are the benefits of concurrent rendering in React? What do you need to do to switch to concurrent rendering when you upgrade to React 18? And what if you don’t want or can’t use concurrent rendering yet?

There are some behavior changes you need to be aware of! In this workshop we will cover all of those subjects and more.

Join me with your laptop in this interactive workshop. You will see how easy it is to switch to concurrent rendering in your React application. You will learn all about concurrent rendering, SuspenseList, the startTransition API and more.
React Hooks Tips Only the Pros Know
React Summit Remote Edition 2021React Summit Remote Edition 2021
177 min
React Hooks Tips Only the Pros Know
Top Content
Featured Workshop
Maurice de Beijer
Maurice de Beijer
The addition of the hooks API to React was quite a major change. Before hooks most components had to be class based. Now, with hooks, these are often much simpler functional components. Hooks can be really simple to use. Almost deceptively simple. Because there are still plenty of ways you can mess up with hooks. And it often turns out there are many ways where you can improve your components a better understanding of how each React hook can be used.You will learn all about the pros and cons of the various hooks. You will learn when to use useState() versus useReducer(). We will look at using useContext() efficiently. You will see when to use useLayoutEffect() and when useEffect() is better.
React, TypeScript, and TDD
React Advanced Conference 2021React Advanced Conference 2021
174 min
React, TypeScript, and TDD
Top Content
Featured WorkshopFree
Paul Everitt
Paul Everitt
ReactJS is wildly popular and thus wildly supported. TypeScript is increasingly popular, and thus increasingly supported.

The two together? Not as much. Given that they both change quickly, it's hard to find accurate learning materials.

React+TypeScript, with JetBrains IDEs? That three-part combination is the topic of this series. We'll show a little about a lot. Meaning, the key steps to getting productive, in the IDE, for React projects using TypeScript. Along the way we'll show test-driven development and emphasize tips-and-tricks in the IDE.
Web3 Workshop - Building Your First Dapp
React Advanced Conference 2021React Advanced Conference 2021
145 min
Web3 Workshop - Building Your First Dapp
Top Content
Featured WorkshopFree
Nader Dabit
Nader Dabit
In this workshop, you'll learn how to build your first full stack dapp on the Ethereum blockchain, reading and writing data to the network, and connecting a front end application to the contract you've deployed. By the end of the workshop, you'll understand how to set up a full stack development environment, run a local node, and interact with any smart contract using React, HardHat, and Ethers.js.
Designing Effective Tests With React Testing Library
React Summit 2023React Summit 2023
151 min
Designing Effective Tests With React Testing Library
Top Content
Featured Workshop
Josh Justice
Josh Justice
React Testing Library is a great framework for React component tests because there are a lot of questions it answers for you, so you don’t need to worry about those questions. But that doesn’t mean testing is easy. There are still a lot of questions you have to figure out for yourself: How many component tests should you write vs end-to-end tests or lower-level unit tests? How can you test a certain line of code that is tricky to test? And what in the world are you supposed to do about that persistent act() warning?
In this three-hour workshop we’ll introduce React Testing Library along with a mental model for how to think about designing your component tests. This mental model will help you see how to test each bit of logic, whether or not to mock dependencies, and will help improve the design of your components. You’ll walk away with the tools, techniques, and principles you need to implement low-cost, high-value component tests.
Table of contents- The different kinds of React application tests, and where component tests fit in- A mental model for thinking about the inputs and outputs of the components you test- Options for selecting DOM elements to verify and interact with them- The value of mocks and why they shouldn’t be avoided- The challenges with asynchrony in RTL tests and how to handle them
Prerequisites- Familiarity with building applications with React- Basic experience writing automated tests with Jest or another unit testing framework- You do not need any experience with React Testing Library- Machine setup: Node LTS, Yarn