I am trying to redirect a user to where he came from, when going to a Login component. For now, actually, I'd be happy just to redirect to a known url. In this case, '/dashboard'.
My application starts with an app.js and the routes are defined as:
render() {
return (
<div>
<Router>
<div>
<Navbar />
<Route exact path='/' component={Home} />
<Route path='/about' component={About} />
<Route path='/contact' component={Contact} />
<Route path="/login" component={Login} />
<Route path="/register" component={Register} />
<Route path="/profile" component={Profile} />
<Route path="/dashboard" component={Dashboard} />
<Route path="/accounts" component={AccountList} />
<Route path="/account" component={Account}/>
</div>
</Router>
</div>
)
}
I have navigated to Login, and the user has entered their credentials. I have validated against a WebAPI call, and have a success.. So I want to redirect.
I am doing this:
message = "Welcome, " + Auth.Auth.firstname();
history.push("/dashboard");
My imports for my login screen has:
import history from '../../helpers/history.js';
Where history.js is simply:
import createHistory from 'history/createHashHistory'
export default createHistory();
The push is executed, but the screen doesn't refresh. The URL simply updates to:
If I click my 'Dashboard' nav button, the URL changes to:
and my screen loads the dashboard.
How should I be redirecting, problematically, when I need to?
createHashHistory? And notimport { createHistory } from 'history'#as in your examplehttp://localhost:8080/login#/dashboardand createHistory for regular urls likehttp://localhost:8080/dashboardthis.props.history.push('/dashboard');. I also don't believe you need to import history at all, as you should get access to it for free.