Export ‘Switch’ (imported as ‘Switch’) was not found in ‘react-router-dom’

Are you encountering the error “attempted import error: ‘switch’ is not exported from ‘react-router-dom'” or variations like “export switch was not found in react-router-dom” or “switch is not exported from react-router”? If so, you’re not alone—this is a common problem that many React developers face when using the ‘react-router-dom’ library.

In this blog post, we’ll explore the causes of this error and provide a few solutions to help you fix it and get your ‘React application up and running.

What Causes the “Export ‘Switch’ (imported as ‘Switch’) was not found in ‘react-router-dom'” Error?

This error occurs when a developer tries to import the ‘Switch’ component from the ‘react-router-dom’ library, but the component is not actually exported by the library. For example, the following code will trigger the error:

import { Switch } from 'react-router-dom';

This error is often confusing for developers because the ‘Switch’ component is a well-known and commonly used component in the ‘react-router’ library.

[Fixed]: ReferenceError: fetch is not defined in NodeJs

However, it’s important to note that the ‘react-router’ and ‘react-router-dom’ libraries are two separate packages, and they have different components that are available for import.

The ‘Switch’ component is a part of the ‘react-router’ library and not the ‘react-router-dom’ library.

Solution 1: Import the Switch Component from the Correct Library

To fix this error, the first solution is to import the ‘Switch’ component from the correct library simply. Instead of importing it from ‘react-router-dom’, you’ll need to import it from the ‘react-router’ library. The correct code would be:

import { Switch } from 'react-router';

This should resolve the “Export ‘Switch’ (imported as ‘Switch’) was not found in ‘react-router-dom'” error and allow you to use the Switch component in your React application as intended.

Solution 2: Use the Routes Component from react-router-dom

If you don’t want to use the ‘Switch’ component, or if you’re using a version of ‘react-router-dom’ that doesn’t support it, you can use the ‘Routes’ component instead.

The Routes component is available in ‘react-router-dom’ and it serves a similar purpose as the ‘Switch’ component – it allows you to specify the set of routes that should be rendered in your React application.

To use the ‘Routes’ component, you’ll need to import it from ‘react-router-dom’ and wrap your ‘Route’ components with it. Here’s an example of how to use the Routes component to fix the “Export ‘Switch’ (imported as ‘Switch’) was not found in ‘react-router-dom'” error:

import React from 'react';
import { BrowserRouter as Router, Route, Link, Routes } from 'react-router-dom';

export default function App() {
  return (
    <Router>
      <div>
        <nav>
          <ul>
            <li>
              <Link to="/">Home</Link>
            </li>
            <li>
              <Link to="/about">About</Link>
            </li>
          </ul>
        </nav>

        {/* Wrap your Route components in a Routes component */}
        <Routes>
          <Route path="/about" element={<About />} />
          <Route path="/" element={<Home />} />
        </Routes>
      </div>
    </Router>
  );
}

function Home() {
  return <h2>Home</h2>;
}

function About() {
  return <h2>About</h2>;
}

Other Tips for Using react-router-dom

If you’re using the ‘react-router-dom’ library in version 6 or higher, there are a few other changes that you should be aware of:

  • To link to other pages in your application, use the Link component from react-router-dom instead of the element.
  • The exact prop has been removed in version 6, and the router will automatically detect the best route for the current URL based on the order of your routes.
  • Instead of passing a children’s prop to the Route components, you’ll need to use the element prop to specify the element that should be rendered for the route.
  • The path format for Route has been simplified in version 6, and you can use dynamic:id params and * wildcards in your paths. The * wildcard can only be used at the end of a path.

Here’s an example of how to use these features in your React application:

import React from 'react';
import { BrowserRouter as Router, Route, Link, Routes } from 'react-router-dom';
import { useParams } from 'react-router-dom';

export default function App() {
  return (
    <Router>
      <div>
        <nav>
          <ul>
            <li>
              <Link to="/">Home</Link>
            </li>
            <li>
              <Link to="/about">About</Link>
            </li>
            <li>
              {/* Link to dynamic path */}
              <Link to="/users/4200">Users</Link>
            </li>
            <li>
              {/* Link to catch all route */}
              <Link to="/does-not-exist">Catch all route</Link>
            </li>
          </ul>
        </nav>

        {/* Wrap your Route components in a Routes component */}
        <Routes>
          <Route path="/about" element={<About />} />
          {/* Handle dynamic path */}
          <Route path="/users/:userId" element={<Users />} />
          <Route path="/" element={<Home />} />
          {/* Only match this when no other routes match */}
          <Route
            path="*"
            element={
              <div>
                <h2>404 Page not found etc</h2>
              </div>
            }
          />
        </Routes>
      </div>
    </Router>
  );
}

function Home() {
  return <h2>Home</h2>;
}

function About() {
  return <h2>About</h2>;
}

function Users() {
  const { userId } = useParams();
  return <h2>User {userId}</h2>;
}

By following these best practices and using the latest version of the ‘react-router-dom’ library, you’ll be able to avoid the “Export ‘Switch’ (imported as ‘Switch’) was not found in ‘react-router-dom'” error and build a successful React application.

Conclusion

In summary, the “Export ‘Switch’ (imported as ‘Switch’) was not found in ‘react-router-dom'” error is a common issue that occurs when trying to import the Switch component from the wrong library.

To fix this error, you can either import the ‘Switch’ component from the correct library or use the ‘Routes’ component from ‘react-router-dom’ as an alternative.

By staying up to date on the latest changes in ‘react-router-dom’ and following best practices, you’ll be able to easily navigate and solve this and other common errors in your React projects.

Leave a Reply