Skip to content

Commit c43fb61

Browse files
committed
[added] <Link hash> prop
Fixes #2176
1 parent 6e89774 commit c43fb61

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

docs/API.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,14 @@ A `<Link>` also knows when the route it links to is active and automatically app
8585

8686
#### Props
8787
##### `to`
88-
The path to link to, e.g., `/users/123`.
88+
The path to link to, e.g. `/users/123`.
8989

9090
##### `query`
9191
An object of key:value pairs to be stringified.
9292

93+
##### `hash`
94+
A hash to put in the URL, e.g. `#a-hash`.
95+
9396
##### `state`
9497
State to persist to the `location`.
9598

modules/Link.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ class Link extends React.Component {
4343
}
4444

4545
static propTypes = {
46-
activeStyle: object,
47-
activeClassName: string,
48-
onlyActiveOnIndex: bool.isRequired,
4946
to: string.isRequired,
5047
query: object,
48+
hash: string,
5149
state: object,
50+
activeStyle: object,
51+
activeClassName: string,
52+
onlyActiveOnIndex: bool.isRequired,
5253
onClick: func
5354
}
5455

@@ -77,16 +78,19 @@ class Link extends React.Component {
7778
}
7879

7980
render() {
80-
const { history } = this.context
81-
const { activeClassName, activeStyle, onlyActiveOnIndex, to, query, state, onClick, ...props } = this.props
81+
const { to, query, hash, state, activeClassName, activeStyle, onlyActiveOnIndex, ...props } = this.props
8282

83+
// Manually override onClick.
8384
props.onClick = (e) => this.handleClick(e)
8485

85-
// Ignore if rendered outside the context
86-
// of history, simplifies unit testing.
86+
// Ignore if rendered outside the context of history, simplifies unit testing.
87+
const { history } = this.context
8788
if (history) {
8889
props.href = history.createHref(to, query)
8990

91+
if (hash)
92+
props.href += hash
93+
9094
if (activeClassName || (activeStyle != null && !isEmptyObject(activeStyle))) {
9195
if (history.isActive(to, query, onlyActiveOnIndex)) {
9296
if (activeClassName)

modules/__tests__/Link-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('A <Link>', function () {
3333
it('knows how to make its href', function () {
3434
class LinkWrapper extends React.Component {
3535
render() {
36-
return <Link to="/hello/michael" query={{ the: 'query' }}>Link</Link>
36+
return <Link to="/hello/michael" query={{ the: 'query' }} hash="#the-hash">Link</Link>
3737
}
3838
}
3939

@@ -43,7 +43,7 @@ describe('A <Link>', function () {
4343
</Router>
4444
), node, function () {
4545
const a = node.querySelector('a')
46-
expect(a.getAttribute('href')).toEqual('/hello/michael?the=query')
46+
expect(a.getAttribute('href')).toEqual('/hello/michael?the=query#the-hash')
4747
})
4848
})
4949

0 commit comments

Comments
 (0)