I have a React app with following simplified structure:
function App(props) {
return (
<Navbar />
<Router>
<Route exact path="/profile/" component={Profile} />
<Route exact path="/" component={Home} />
</Router>
);
}
In Navbar component I have a button for logout, meaning that I need to:
- Delete Authentication token from Storage (simply do a logic for general case), and
- Route user to the login page.
I tried to use onClick of that button and use this.props.history.push('/') to change the path, but this doesn't work (it complains that history is undefined), since my navbar is defined outside the Router.
My question is, is it okay to move Navbar to inside of my Router components and repeat it while it is the same in all of pages? Or is there any duplicate-proof solution I can use to change my browser path?