I'm learning React and I'm trying to create a quiz app. However, I am having issues with redirecting to another component on button click.
I keep getting this error
useNavigate() may be used only in the context of a <Router> component.
This is my code
App.js
import React from 'react';
import Home from "./component/Home";
export default function App() {
return (
<main>
<Home />
</main>
);
}
component/Home.js
import React from "react";
import ResponsiveAppBar from "./ResponsiveAppBar";
import {
BrowserRouter as Router,
Routes,
Route,
useNavigate,
} from "react-router-dom";
import Typography from "@mui/material/Typography";
import Avatar from "@mui/material/Avatar";
import Button from "@mui/material/Button";
import img from "./../assets/images/earth.jpg";
const Home = (props) => {
let navigate = useNavigate();
function handleClick() {
navigate("/Learn");
}
function secondHandleClick() {
navigate("/Quiz");
}
return (
<div>
<Router>
<ResponsiveAppBar />
<Typography variant="h6" component="div">
EARTH
</Typography>
<Avatar
variant={"rounded"}
alt="The Earth"
src={img}
style={{
width: 200,
height: 200,
}}
/>
{/* <img src={'/assets/images/earth.jpg'} alt ='The Earth'/> */}
<Typography variant="h6" component="div">
Earth apart from being our home, has a lot of cool fun facts you are
probably not aware of. Not to worry, we are gonna get to know some of
these facts today. Click the Learn button to start or the Quiz button
to check how much you already know.
</Typography>
<Routes>
<Button variant="contained" onClick={handleClick}>
Learn
</Button>
<Button variant="contained" onClick={secondHandleClick}>
Quiz
</Button>
</Routes>
</Router>
</div>
);
};
export default Home;
Where am I missing it? I'd appreciate some help.