I'm trying to perform a redirect in a react component using react-router 1.0.2.
In my main app.js
I create a hash history:
import createHashHistory from 'history/lib/createHashHistory';
const history = createHashHistory();
...
ReactDOM.render(
<RelayRouter history={history} routes={routes} />,
document.getElementById('root')
);
When I perform a database insert, I want to redirect to another page. I was using this which worked in an earlier version, but doesn't work any more:
import createHashHistory from 'history/lib/createHashHistory';
const history = createHashHistory();
...
// inside Component after a successful DB insert
history.pushState(null, `/#/albums/${newAlbum.id}`, null);
This is in another component (CreateAlbum
).
Do I need to call pushState
on the same history instance, and if so how can I get hold of it from within a component? If not, what am I doing wrong? The location bar does get updated, but the page stays the same.
Finally, I've manually prefixed /#/
to the URL. This is sloppy and will only work with a Hash History. I saw in the API there's a createHref
method, but it expects a pathname which isn't explained. Given that the relevant route is defined as follows, how can I use that to generate a URL in the correct form as well?
<Route
component={App}
queries={ViewerQueries}
path="/">
<Route
path="/create" component={CreateAlbum} />
<Route
path="/albums/:albumId" component={AlbumDetails}
queries={ViewerQueries} />
</Route>