button onclick to redirect react code example
Example 1: redirect onclick react
import React from 'react';
import { useHistory } from "react-router-dom";
function Home() {
const history = useHistory();
const handleRoute = () =>{
history.push("/about");
}
return (
<Button
onClick={handleRoute}>
About
</Button>
);
}
export default Home;
Example 2: react-router-dom redirect on click
import { useHistory } from 'react-router-dom';
function app() {
let history = useHistory();
const redirect = () => {
history.push('/your-path')
}
return (
<div>
<button onClick={redirect}>Redirect</button>
</div>
)
}