I am experimenting with react-router-dom-6 and I have trouble going back to previous pages.
The sample app can Go Back from the "Report Page" to "About Page" However, it can't Go Back from the Report Page to the Home Page. The url won't switch.
import {
BrowserRouter as Router,
Route,
Routes,
useNavigate
} from "react-router-dom";
import ReactDOM from "react-dom";
const Home = () => {
const navigate = useNavigate();
return (
<div>
<h1>Home Page</h1>
<button onClick={() => navigate("/about")}>To about</button>
</div>
);
};
const About = () => {
const navigate = useNavigate();
return (
<div>
<div>About Page</div>
<button onClick={() => navigate("/report")}>Go to Report</button>
<button onClick={() => navigate(-1)}>Go back</button>
</div>
);
};
const Report = () => {
const navigate = useNavigate();
return (
<div>
<div>Report Page</div>
<button onClick={() => navigate(-1)}>Go back</button>
</div>
);
};
const rootElement = document.getElementById("root");
ReactDOM.render(
<Router>
<Routes>
<Route path="/" element={<Home />} />
<Route path="about" element={<About />} />
<Route path="report" element={<Report />} />
</Routes>
</Router>,
rootElement
);