File size: 1,057 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# detect-history-navigation

This module determines when a page has been rendered via the history event (back or forward buttons, or via the history methods) versus being a newly navigated page (e.g., via clicking a link to a page).

This can be used alongside the [Calypso Router module](../../../packages/calypso-router) by calling the `start()` method of this module _prior_ to the `start()` method of `page.js`.

```js
import page from '@automattic/calypso-router';
import detectHistoryNavigation from 'detect-history-navigation';

detectHistoryNavigation.start();
page.start();
```

Then controllers and components can call `detectHistoryNavigation.loadedViaHistory()` which returns a boolean determining whether the current route is being loaded from a history event or is a fresh pageload.

Components that are mounted into the initial view of a page that is loaded by history can detect this in `componentWillMount`, `componentDidMount`, and the initial run of their `render` method. After the initial render `loadedViaHistory()` will return false.