How to Fix the ‘switch’ is Not Exported from react-router-dom Error

Encountering the ‘switch’ is not exported from ‘react-router-dom’ error can abruptly stop your development. This common issue arises when your project uses React Router version 6 or newer, as the library has replaced the older `Switch` component. This guide will walk you through why this error occurs, how to verify your library version, and the simple steps to update your code using the new `Routes` component, ensuring your application’s navigation works perfectly.

Why Does This Import Error Occur?

The root cause of this error is a version mismatch. Your application’s code is written using syntax from an older version of `react-router-dom` (specifically, version 5 or earlier), but your project has a newer version installed.

In version 6, the React Router team introduced significant improvements to make routing more intuitive and powerful. As part of this major update, they deprecated the “ component and introduced a smarter replacement called “.

Therefore, when your code tries to `import { Switch }` from `react-router-dom` v6, the library reports that ‘switch’ is not an exported module because it no longer exists. This is a breaking change, meaning older code is not compatible without modifications.

Understanding the Evolution of React Router

`react-router-dom` is an essential library for any React single-page application (SPA). It handles client-side routing, allowing you to create a seamless user experience where users can navigate between different views or pages without a full page reload from the server.

The library provides core components that developers use to build navigation systems. Over the years, it has evolved to keep up with best practices in the React ecosystem. The transition from version 5 to version 6 marked one of its most significant updates.

This update aimed to simplify the API, improve route matching algorithms, and make features like nested routes easier to implement. Understanding this evolution is key to writing modern, maintainable React code.

How to Quickly Check Your ‘react-router-dom’ Version

Before making any changes, you should first confirm which version of `react-router-dom` your project is using. This is a simple but crucial diagnostic step. You can easily check this from your project’s terminal.

Open your terminal, navigate to your project’s root directory, and run the following command:

npm list react-router-dom

If you are using Yarn, you can use `yarn list –pattern “react-router-dom”`. The output will display the installed version number. If the version number starts with a ‘6’ (e.g., 6.4.2), you must replace “ with “. This confirmation removes any guesswork and points you directly to the correct solution.

The Modern Solution: Introducing the ‘Routes’ Component

The “ component is the modern replacement for “ in `react-router-dom` v6. While it serves a similar purpose of wrapping your individual “ definitions, it functions more intelligently.

Unlike “, which would render the first child “ that matched the URL, “ uses a more advanced route matching algorithm to pick the best possible match. This makes complex routing scenarios, especially those involving nested routes, much easier to manage and less prone to errors.

Additionally, the syntax for defining routes has been streamlined. Migrating to “ not only fixes the import error but also aligns your project with the current best practices for React development, making your code cleaner and more future-proof.

Step-by-Step Guide to Migrating from ‘Switch’ to ‘Routes’

Updating your code to fix the error is straightforward. Follow these steps to migrate your routing setup from the old syntax to the new v6 syntax.

  1. Update Your Import Statement: Go to the file where your routes are defined. Find the line that imports from `react-router-dom` and change it. Remove `Switch` and add `Routes`.
  2. Replace the Component Tags: In your JSX, replace the opening “ tag with “ and the closing “ tag with “.
  3. Update the Route Prop: This is a critical change. In v6, the “ component no longer uses the `component` or `render` props. Instead, you must use the `element` prop and pass it a JSX element. For example, `component={Home}` becomes `element={}`.

By following these three steps, you will resolve the import error and successfully update your application to use the modern React Router API.

Code Comparison: Before (v5) and After (v6)

Visual examples make the required changes much clearer. The table below shows a direct comparison of a basic routing setup in `react-router-dom` version 5 versus version 6.

Old Syntax (react-router-dom v5)New Syntax (react-router-dom v6)
import { Switch, Route } from 'react-router-dom';

function App() {
  return (
    <Switch>
      <Route exact path="/" component={Home} />
      <Route path="/about" component={About} />
    </Switch>
  );
}
import { Routes, Route } from 'react-router-dom';

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

Notice that the `exact` prop is also no longer needed in most cases in v6, as the new algorithm is better at picking the most specific path.

Frequently Asked Questions

What does the error “‘switch’ is Not Exported From ‘react-router-dom'” mean?
This error means you are trying to use the “ component with version 6 or newer of the `react-router-dom` library. This component was removed in version 6 and replaced by the “ component.

How can I fix the ‘Switch’ error in React Router version 6?
To fix it, you need to update your code. First, change your import statement from `import { Switch }` to `import { Routes }`. Then, replace the “ tags with “ tags in your JSX and update your “ components to use the `element` prop instead of the `component` prop.

Can I downgrade my version of react-router-dom to keep using ‘Switch’?
Yes, you can downgrade to version 5 by running `npm install react-router-dom@5`. However, this is not recommended, as you will miss out on the performance improvements, security updates, and new features of the latest version.

Are there other major changes in React Router 6?
Yes, besides replacing `Switch`, v6 introduced a simpler API for nested routes, new hooks like `useNavigate` to replace `useHistory`, and an overall smaller bundle size. It’s best to review the official migration guide to understand all the changes.

Where can I find the official migration guide for React Router?
The official migration guide is the best resource for a comprehensive overview of all changes. You can find it on the official React Router website, which provides detailed explanations and code examples to help you upgrade from version 5 to version 6.