Skip to content

Commit 7198cd9

Browse files
committed
Implement NavHashLink
1 parent c424f2e commit 7198cd9

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

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)