You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+30-8Lines changed: 30 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -1,23 +1,29 @@
1
1
# React Router Hash Link
2
2
3
-
**_Note that this is for React Router v4/5, for v2/3 see [this solution](https://github.com/rafgraph/react-router-hash-link/tree/react-router-v2/3)._**
This is a solution to [React Router's issue of not scrolling to `#hash-fragments`](https://github.com/reactjs/react-router/issues/394#issuecomment-220221604) when using the `<Link>` component to navigate.
6
6
7
-
[Demo website](https://react-router-hash-link.rafgraph.dev/) (code on the [`gh-pages` branch](https://github.com/rafgraph/react-router-hash-link/tree/gh-pages))
7
+
When you click on a link created with `react-router-hash-link` it will scroll to the element on the page with the `id` that matches the `#hash-fragment` in the link. This will also work for elements that are created after an asynchronous data load. Note that you must use React Router's `BrowserRouter` for this to work.
8
8
9
9
---
10
10
11
-
This is a solution to [React Router's issue of not scrolling to `#hash-fragments`](https://github.com/reactjs/react-router/issues/394#issuecomment-220221604) when using the `<Link>` component to navigate.
11
+
### [Live demo app for React Router Hash Link](https://react-router-hash-link.rafgraph.dev)
12
12
13
-
When you click on a link created with `react-router-hash-link` it will scroll to the element on the page with the `id` that matches the `#hash-fragment` in the link. This will also work for elements that are created after an asynchronous data load. Note that you must use React Router's `BrowserRouter` for this to work.
13
+
Code is in the [`/demo`](/demo) folder.
14
+
15
+
---
16
+
17
+
## Basics
14
18
15
19
```shell
16
-
$ yarn add react-router-hash-link
17
-
# OR
18
-
$ npm install --save react-router-hash-link
20
+
npm install --save react-router-hash-link
19
21
```
20
22
23
+
`react-router-dom` is a peer dependency.
24
+
25
+
---
26
+
21
27
### `<HashLink>`
22
28
23
29
```javascript
@@ -29,6 +35,8 @@ import { HashLink } from 'react-router-hash-link';
29
35
<HashLink to="/some/path#with-hash-fragment">Link to Hash Fragment</HashLink>
30
36
```
31
37
38
+
---
39
+
32
40
### `<NavHashLink>`
33
41
34
42
```javascript
@@ -46,6 +54,8 @@ import { NavHashLink } from 'react-router-hash-link';
46
54
>Link to Hash Fragment</NavHashLink>
47
55
```
48
56
57
+
---
58
+
49
59
## Scrolling API
50
60
51
61
### `smooth: boolean`
@@ -61,6 +71,8 @@ import { HashLink } from 'react-router-hash-link';
61
71
</HashLink>;
62
72
```
63
73
74
+
---
75
+
64
76
### `scroll: function`
65
77
66
78
- Custom scroll function called with the element to scroll to, e.g. `const myScrollFn = element => {...}`
@@ -76,6 +88,8 @@ import { HashLink } from 'react-router-hash-link';
76
88
</HashLink>;
77
89
```
78
90
91
+
---
92
+
79
93
### Scroll to top of page
80
94
81
95
- To scroll to the top of the page set the hash fragment to `#` (empty) or `#top`
@@ -88,16 +102,22 @@ import { HashLink } from 'react-router-hash-link';
88
102
<HashLink to="#top">Link to Top of Page</HashLink>
89
103
```
90
104
105
+
---
106
+
91
107
### Scroll with offset
92
108
93
109
- To scroll with offset use a custom scroll function, one way of doing this can be found [here](https://github.com/rafgraph/react-router-hash-link/issues/25#issuecomment-536688104)
94
110
111
+
---
112
+
95
113
### `elementId: string`
96
114
97
115
- Scroll to the element with matching id
98
116
- Used instead of providing a hash fragment as part of the `to` prop, if both are present then the `elementId` will override the `to` prop's hash fragment
99
117
- Note that it is generally recommended to use the `to` prop's hash fragment instead of the `elementId`
100
118
119
+
---
120
+
101
121
## Custom `Link`
102
122
103
123
The exported components are wrapped versions of the `Link` and `NavLink` exports of react-router-dom. In some cases you may need to provide a custom `Link` implementation.
@@ -118,6 +138,8 @@ const MyComponent = () => (
118
138
);
119
139
```
120
140
141
+
---
142
+
121
143
## Focus Management
122
144
123
145
`react-router-hash-link` attempts to recreate the native browser focusing behavior as closely as possible.
0 commit comments