react router dom programmatically change route code example
Example 1: Programmatically navigate using react router
import { useHistory } from "react-router-dom";
function HomeButton() {
let history = useHistory();
function handleClick() {
history.push("/home");
}
return (
<button type="button" onClick={handleClick}>
Go home
</button>
);
}
Example 2: react router change route from function
import {withRouter} from 'react-router-dom';
this.props.history.push('route');
export default withRouter(your component name)
the react component should be wrapped with withRouter hoc
so that history will work