react router dom route component code example

Example 1: router in react-router-dom

npm install react-router-dom
import {BrowserRouter, Switch, Route, Link} from "react-router-dom"
import React from "react";
import {
  BrowserRouter as Router,
} from "react-router-dom";

export default function App() {
  return (
              <Link to="/">Home</Link>
              <Link to="/about">About</Link>
              <Link to="/users">Users</Link>

        {/* A <Switch> looks through its children <Route>s and
            renders the first one that matches the current URL. */}
          <Route path="/about">
            <About />
          <Route path="/users">
            <Users />
          <Route path="/">
            <Home />

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

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

function Users() {
  return <h2>Users</h2>;

Example 2: route in component react

import { BrowserRouter, Route, Link } from "react-router-dom";