Skip to content

Commit fd58cae

Browse files
committed
Merge branch 'nav-hash-link'
2 parents c424f2e + b709585 commit fd58cae

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,31 @@ This is a solution to [React Router's issue of not scrolling to `#hash-fragments
99
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.
1010

1111
```shell
12+
$ yarn add react-router-hash-link
13+
# OR
1214
$ npm install --save react-router-hash-link
1315
```
1416

17+
## `<HashLink>`
1518
```javascript
1619
// In YourComponent.js
1720
...
1821
import { HashLink as Link } from 'react-router-hash-link';
1922
...
20-
// Use it just like a RRv4 link (to can be a string or an object, see RRv4 api for details):
23+
// Use it just like a RRv4 <Link> (to can be a string or an object, see RRv4 api for details):
2124
<Link to="/some/path#with-hash-fragment">Link to Hash Fragment</Link>
2225
```
26+
27+
28+
## `<NavHashLink>`
29+
```javascript
30+
// In YourComponent.js
31+
...
32+
import { NavHashLink as NavLink } from 'react-router-hash-link';
33+
...
34+
// Use it just like a RRv4 <NavLink> (see RRv4 api for details):
35+
<NavLink
36+
to="/some/path#with-hash-fragment"
37+
activeClassName="selected"
38+
>Link to Hash Fragment</NavLink>
39+
```

src/index.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3-
import { Link } from 'react-router-dom';
3+
import { Link, NavLink } from 'react-router-dom';
44

55
let hashFragment = '';
66
let observer = null;
@@ -41,7 +41,7 @@ function hashLinkScroll() {
4141
}, 0);
4242
}
4343

44-
export function HashLink(props) {
44+
export function genericHashLink(props, As) {
4545
function handleClick(e) {
4646
reset();
4747
if (props.onClick) props.onClick(e);
@@ -52,14 +52,25 @@ export function HashLink(props) {
5252
}
5353
if (hashFragment !== '') hashLinkScroll();
5454
}
55-
return <Link {...props} onClick={handleClick}>{props.children}</Link>;
55+
return <As {...props} onClick={handleClick}>{props.children}</As>;
56+
}
57+
58+
export function HashLink(props) {
59+
return genericHashLink(props, Link);
5660
}
5761

58-
HashLink.propTypes = {
62+
export function NavHashLink(props) {
63+
return genericHashLink(props, NavLink);
64+
}
65+
66+
const propTypes = {
5967
onClick: PropTypes.func,
6068
children: PropTypes.node,
6169
to: PropTypes.oneOfType([
6270
PropTypes.string,
6371
PropTypes.object,
6472
]),
6573
};
74+
75+
HashLink.propTypes = propTypes;
76+
NavHashLink.propTypes = propTypes;

0 commit comments

Comments
 (0)