|
| 1 | +API: `Location` (object) |
| 2 | +======================== |
| 3 | + |
| 4 | +The router uses "location" objects to determine the current state of |
| 5 | +the application and update it when needed. In a browser environment, |
| 6 | +the location represents the URL in the browser's location bar. On the |
| 7 | +server, the location is the URL path that was used in the request. |
| 8 | + |
| 9 | +ReactRouter ships with several locations that make it convenient to |
| 10 | +use in many different scenarios. They are listed below. |
| 11 | + |
| 12 | + | `HashLocation` | Stores the current URL in `window.location.hash`. Use this if you need to support browsers that do not support the HTML5 history API | |
| 13 | + | `HistoryLocation` | Uses the HTML5 history API to store state in clean URLs | |
| 14 | + | `RefreshLocation` | A fallback for having clean URLs in browsers that do not support the HTML5 history API by updating `window.location` whenever the route changes. The router automatically falls back to this when you try to use `HistoryLocation` in old browsers | |
| 15 | + | `StaticLocation` | A location for stateless environments (like servers) where the URL is given once | |
| 16 | + | `TestLocation` | A location that allows you to easily stub out URL history when writing tests | |
| 17 | + |
| 18 | +You can also supply the router with your own location implementation. The |
| 19 | +following methods must be implemented: |
| 20 | + |
| 21 | +Methods |
| 22 | +------- |
| 23 | + |
| 24 | +### `addChangeListener(listener)` |
| 25 | + |
| 26 | +Adds a function to the location that should be called when it changes. |
| 27 | + |
| 28 | +### `removeChangeListener(listener)` |
| 29 | + |
| 30 | +Stop calling the given function when the location changes. |
| 31 | + |
| 32 | +### `push` |
| 33 | + |
| 34 | +Called when the router is transitioning from one path to another. |
| 35 | + |
| 36 | +### `replace` |
| 37 | + |
| 38 | +Called when the router is replacing (not transitioning) one url with |
| 39 | +another. |
| 40 | + |
| 41 | +### `pop` |
| 42 | + |
| 43 | +Called when the router attempts to go back one entry in the history. |
| 44 | + |
| 45 | +### `getCurrentPath` |
| 46 | + |
| 47 | +Should return the current URL path, complete with query string (if applicable). |
| 48 | +This method should be ready to go immediately after setup. |
| 49 | + |
| 50 | +### `toString` |
| 51 | + |
| 52 | +Should return a useful string for logging and debugging. |
| 53 | + |
| 54 | +History |
| 55 | +------- |
| 56 | + |
| 57 | +Additionally, location objects must: |
| 58 | + |
| 59 | +- Increment `ReactRouter.History.length` immediately **after** the URL changes |
| 60 | +- Decrement `ReactRouter.History.length` immediately **before** going back to the |
| 61 | + previous URL |
| 62 | + |
| 63 | +Please refer to the [built-in location objects][locations] to get an idea for how this is done. |
| 64 | + |
| 65 | +[locations]: /locations |
0 commit comments