react-history provides tools to manage session history using React. It's a thin wrapper around the
history package. In web browsers, this library also transparently manages changes to the URL which makes it easier for creators of single-page applications to support things like bookmarks and the back button.
Note: This library is highly experimental.
$ npm install --save react-history
Then with a module bundler like webpack, use as you would anything else:
// using ES6 modules// using CommonJS modulesvar BrowserHistory = BrowserHistory
The UMD build is also available on unpkg:
You can find the library on
react-history ships with 3 different history components that you can use depending on your environment.
<BrowserHistory>is for use in modern web browsers that support the HTML5 history API (see cross-browser compatibility)
<MemoryHistory>is used as a reference implementation and may also be used in non-DOM environments, like React Native
<HashHistory>is for use in legacy web browsers
Depending on the method you want to use to keep track of history, you'll
require) one of these methods directly from the package root (i.e.
history/BrowserHistory). For the sake of brevity, the term
<History> in this document refers to any of these implementations.
Basic usage looks like this:
const App = React
The props for each
<History>, along with their default values are:
<BrowserHistorybasename="" // The base URL of the app (see below)forceRefresh=false // Set true to force full page refresheskeyLength=6 // The length of location.key// A function to use to confirm navigation with the user (see below)getUserConfirmation=/><MemoryHistoryinitialEntries= '/' // The initial URLs in the history stackinitialIndex=0 // The starting index in the history stackkeyLength=6 // The length of location.key// A function to use to confirm navigation with the user. Required// if you return string prompts from transition hooks (see below)getUserConfirmation=null/><HashHistorybasename="" // The base URL of the app (see below)hashType="slash" // The hash type to use (see below)// A function to use to confirm navigation with the user (see below)getUserConfirmation=/>
<History> elements call their
children function every time the URL changes.
<History><div><p>The current URL is locationpathnamelocationsearchlocationhash</p><p>You arrived at this URL via a action action</p></div></History>
action properties represent the current URL and how we got there.
react-history also provides the following components that may be used to modify the current URL:
<Push>pushes a new entry onto the history stack
<Replace>replaces the current entry on the history stack with a new one
<Pop>modifies the current pointer or index into the history stack
<Back>moves back one entry in the history, shorthand for
<Forward>moves forward one entry in the history, shorthand for
These components are called "action" components because they modify the URL. When any of these are rendered, the URL updates and
<History> objects emit a new location.
<Replace> accept either:
// Push a new entry onto the history stack.<Push path="/home?the=query#the-hash" state= some: 'state' />// Use a location-like object to push a new entry onto the stack.<Push location=pathname: '/home'search: '?the=query'hash: '#the-hash'state: some: 'state'/>
Note: Location state is not supported using
For example, you could build a very simple
<Link> component using a
const Link = React
<Link> implementation is for demonstration purposes only. It is not accessible and does not include many of the nice features of a real hyperlink. If you're looking for a proper
<Link> implementation, please use
react-history lets you register a prompt message that will be shown to the user before location listeners are notified. This allows you to make sure the user wants to leave the current page before they navigate away. You do this by rendering a
const Form = React
Note: You'll need to provide a
getUserConfirmation prop to use
<MemoryHistory> (see the
Using a Base URL
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.
// All URLs transparently have the "/the/base" prefix.<History basename="/the/base">// When the URL is /the/base/home, location.pathname is just /home.<p>The current pathname is locationpathname</p></History>
basename is not suppported in
<MemoryHistory> where you have full control over all your URLs.
Forcing Full Page Refreshes in
<BrowserHistory> uses HTML5
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
Modifying the Hash Type in
<HashHistory> uses a leading slash in hash-based URLs. You can use the
hashType option to use a different hash formatting.
// The default is to add a leading / to all hashes, so your URLs// are like /#/inbox/5. This is also know as the "slash" hash type.<HashHistory hashType="slash"/>// You can also omit the leading slash using the "noslash" hash type.// This gives you URLs like /#inbox/5.<HashHistory hashType="noslash"/>// Support for Google's legacy AJAX URL "hashbang" format gives you// URLs like /#!/inbox/5.<HashHistory hashType="hashbang"/>
Thanks to BrowserStack for providing the infrastructure that allows us to run our build in real browsers.