If all the URLs in your app are relative to some other "base" URL, use the basename
option. This option transparently adds the given string to the front of all URLs you use.
const history = createHistory({
basename: '/the/base'
});
history.listen(location => {
console.log(location.pathname); // /home
});
history.push('/home'); // URL is now /the/base/home
Note: basename
is not supported in createMemoryHistory
.
By default createBrowserHistory
uses HTML5 pushState
and replaceState
to prevent reloading the entire page from the server while navigating around. If instead you would like to reload as the URL changes, use the forceRefresh
option.
const history = createBrowserHistory({
forceRefresh: true
});
By default createHashHistory
uses a leading slash in hash-based URLs. You can use the hashType
option to use a different hash formatting.
const history = createHashHistory({
hashType: 'slash' // the default
});
history.push('/home'); // window.location.hash is #/home
const history = createHashHistory({
hashType: 'noslash' // Omit the leading slash
});
history.push('/home'); // window.location.hash is #home
const history = createHashHistory({
hashType: 'hashbang' // Google's legacy AJAX URL format
});
history.push('/home'); // window.location.hash is #!/home