I have a functional component with a react-router.
Working fine when using <Link ... />
but I want to redirect on some event.
The problem is that props.history
is undefined.
function App(props) {
const onClick = () => {
console.log(props.history);
//props.history.push("/about");
};
return (
<div className="App">
<BrowserRouter>
<Link to="/about">About</Link> <br />
<button onClick={onClick}>
<b>onclick redirect</b>
</button>
<Route path="/about" component={About} />
</BrowserRouter>
</div>
);
}
https://codesandbox.io/s/react-router-demo-o6s89
How can I redirect programmatically?
Thanks