https://github.com/ReactTraining/react-history
Manage session history with React
https://github.com/ReactTraining/react-history
Last synced: 9 months ago
JSON representation
Manage session history with React
- Host: GitHub
- URL: https://github.com/ReactTraining/react-history
- Owner: ReactTraining
- Created: 2016-08-15T19:01:19.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-05-31T10:01:55.000Z (almost 8 years ago)
- Last Synced: 2025-07-24T00:56:17.284Z (9 months ago)
- Language: JavaScript
- Homepage:
- Size: 122 KB
- Stars: 268
- Watchers: 7
- Forks: 23
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
Awesome Lists containing this project
README
# react-history [![Travis][build-badge]][build] [![npm package][npm-badge]][npm]
[build-badge]: https://img.shields.io/travis/ReactTraining/react-history/master.svg?style=flat-square
[build]: https://travis-ci.org/ReactTraining/react-history
[npm-badge]: https://img.shields.io/npm/v/react-history.svg?style=flat-square
[npm]: https://www.npmjs.com/package/react-history
[`react-history`](https://www.npmjs.com/package/react-history) provides tools to manage session history using [React](https://facebook.github.io/react). It's a thin wrapper around the [`history`](https://www.npmjs.com/package/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.
## Installation
Using [npm](https://www.npmjs.com/):
$ npm install --save react-history
Then with a module bundler like [webpack](https://webpack.github.io/), use as you would anything else:
```js
// using ES6 modules
import { BrowserHistory } from "react-history";
// using CommonJS modules
var BrowserHistory = require("react-history").BrowserHistory;
```
The UMD build is also available on [unpkg](https://unpkg.com):
```html
```
You can find the library on `window.ReactHistory`.
## Usage
`react-history` ships with 3 different history components that you can use depending on your environment.
* `` is for use in modern web browsers that support the [HTML5 history API](http://diveintohtml5.info/history.html) (see [cross-browser compatibility](http://caniuse.com/#feat=history))
* `` is used as a reference implementation and may also be used in non-DOM environments, like [React Native](https://facebook.github.io/react-native/)
* `` is for use in legacy web browsers
Depending on the method you want to use to keep track of history, you'll `import` (or `require`) one of these methods directly from the package root (i.e. `history/BrowserHistory`). For the sake of brevity, the term `` in this document refers to any of these implementations.
Basic usage looks like this:
```js
import History from "react-history/BrowserHistory";
const App = React.createClass({
render() {
return (
{({ history, action, location }) => (
The current URL is {location.pathname}
{location.search}
{location.hash}. You arrived at this URL via a {action} action.
)}
);
}
});
```
The props for each ``, along with their default values are:
```js
callback(window.confirm(message))}
/>
callback(window.confirm(message))}
/>
```
### Listening
`` elements call their `children` function every time the URL changes.
```js
{({ history, action, location }) => (
The current URL is {location.pathname}
{location.search}
{location.hash}.
You arrived at this URL via a {action} action.
)}
```
The `history` object is the same object you'd get if you created your own [`history` object](https://npmjs.com/package/history) directly. Please refer to [the `history` docs](https://github.com/ReactTraining/history/blob/master/README.md) for more information on how to use it.
The `location` and `action` properties represent the current URL and how we got there.
### Navigation
`react-history` also provides the following components that may be used to modify the current URL:
* `` pushes a new entry onto the history stack
* `` replaces the current entry on the history stack with a new one
* `` modifies the current pointer or index into the history stack
* `` moves back one entry in the history, shorthand for ``
* `` 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 `` objects emit a new location.
`` and `` accept either:
* `path` and `state` props _or_
* a `location` prop
```js
// Push a new entry onto the history stack.
// Use a location-like object to push a new entry onto the stack.
```
**Note:** Location state is not supported using ``.
For example, you could build a very simple `` component using a ``:
```js
import React from "react";
import PropTypes from "prop-types";
import { Push } from "react-history/Actions";
const Link = React.createClass({
propTypes: {
to: PropTypes.string.isRequired
},
getInitialState() {
return { wasClicked: false };
},
render() {
const { to, ...props } = this.props;
// If the was clicked, update the URL!
if (this.state.wasClicked) return ;
return (
this.setState({ wasClicked: true })} />
);
}
});
```
**Note:** This `` 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 `` implementation, [please use `react-router`](https://www.npmjs.com/package/react-router).
### Blocking Transitions
`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 `` component.
```js
import Prompt from "react-history/Prompt";
const Form = React.createClass({
getInitialState() {
return { inputText: "" };
},
handleChange(event) {
this.setState({ inputText: event.target.value });
},
render() {
const { inputText } = this.state;
return (
);
}
});
```
**Note:** You'll need to provide a `getUserConfirmation` prop to use ``s with `` (see [the `history` docs](https://github.com/ReactTraining/history#customizing-the-confirm-dialog)).
### 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.
```js
// All URLs transparently have the "/the/base" prefix.
{({ location }) => (
// When the URL is /the/base/home, location.pathname is just /home.
The current pathname is {location.pathname}.
)}
```
**Note:** `basename` is not suppported in `` where you have full control over all your URLs.
### Forcing Full Page Refreshes in ``
By default `` 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.
```js
```
### Modifying the Hash Type in ``
By default `` uses a leading slash in hash-based URLs. You can use the `hashType` option to use a different hash formatting.
```js
// 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.
// You can also omit the leading slash using the "noslash" hash type.
// This gives you URLs like /#inbox/5.
// Support for Google's legacy AJAX URL "hashbang" format gives you
// URLs like /#!/inbox/5.
```
## Thanks
Thanks to [BrowserStack](https://www.browserstack.com/) for providing the infrastructure that allows us to run our build in real browsers.