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
// Use it just like a RRv4/5 <NavLink> (see RRv4/5 api for details):
39
-
<NavLink
38
+
// Use it just like a RRv4/5 <NavLink> (see RRv4/5 api for details)
39
+
// It will be active only if both the path and hash fragment match
40
+
<NavHashLink
40
41
to="/some/path#with-hash-fragment"
41
42
activeClassName="selected"
43
+
activeStyle={{ color:'red' }}
42
44
// etc...
43
-
>Link to Hash Fragment</NavLink>
45
+
>Link to Hash Fragment</NavHashLink>
44
46
```
45
47
46
48
## Scrolling API
@@ -49,22 +51,39 @@ import { NavHashLink as NavLink } from 'react-router-hash-link';
49
51
- React Router Hash Link uses the native Element method `element.scrollIntoView()` for scrolling, and when the `smooth` prop is present it will call it with the smooth option, `element.scrollIntoView({ behavior: 'smooth' })`
50
52
- Note that not all browsers have implemented options for `scrollIntoView` - see [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView) and [Can I Use](https://caniuse.com/#feat=scrollintoview) - there is also a browser [polyfill for smooth scrolling](https://github.com/iamdustan/smoothscroll) which you can install separately so `smooth` will work in all browsers
- To scroll to the top of the page set the hash fragment to `#` (empty) or `#top`
71
+
- This is inline with the [HTML spec](https://html.spec.whatwg.org/multipage/browsing-the-web.html#target-element), also see [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#Linking_to_an_element_on_the_same_page)
72
+
```js
73
+
import { HashLink } from'react-router-hash-link';
74
+
<HashLink to="/path#top">Link to Top of Page</HashLink>
75
+
```
76
+
77
+
### Scroll with offset
78
+
- 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)
79
+
80
+
### `elementId: string`
81
+
- Scroll to the element with matching id
82
+
- 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
83
+
- Note that it is generally recommended to use the `to` prop's hash fragment instead of the `elementId`
84
+
85
+
86
+
## Custom `Link`
68
87
69
88
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.
0 commit comments