I have a requirement to include both BrowserRouter and HashRouter for different routes.
For this I have written this basic code to understand combining both:
import React from "react";
import {
BrowserRouter as Router,
Switch,
Route,
Link,
HashRouter
} from "react-router-dom";
export default function BasicExample() {
return (
<Router>
<div>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
<li>
<Link to="/dashboard">Dashboard</Link>
</li>
</ul>
<Switch>
<Route exact path="/">
<Home />
</Route>
<Route exact path="/about">
<HashRouter>
<Route path="/about" >
<About />
</Route>
</HashRouter>
</Route>
<Route path="/dashboard">
<Dashboard />
</Route>
</Switch>
</div>
</Router>
);
}
This is the Home component
function Home() {
return (
<div>
<h2>Home</h2>
</div>
);
}
This is the About component which has HashRouter
function About() {
return (
<div>
<h2>About</h2>
</div>
);
}
This is again a normal component with BrowserRouter
function Dashboard() {
return (
<div>
<h2>Dashboard</h2>
</div>
);
}
And when I click on the About link then it changes the URL but does not display the "About" text.