Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow onClick on next/link component's child (#4474)
Allow `onClick` on `next/link` child. This should not be a breaking change, but it's a very useful feature. Real-life use cases include: analytics or closing menu on navigation, and other. - [x] allow optional `onClick` on `next/link` component's child - [x] call original `child.props.onClick(e)` before `this.linkClicked(e)` - [x] add integration tests - [x] cancel the navigation if `e.defaultPrevented === true` Fixes #1490
- Loading branch information
1 parent
832c494
commit 6692252
Showing
4 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Component } from 'react' | ||
import Link from 'next/link' | ||
|
||
let count = 0 | ||
|
||
export default class OnClick extends Component { | ||
static getInitialProps ({ res }) { | ||
if (res) return { count: 0 } | ||
count += 1 | ||
|
||
return { count } | ||
} | ||
|
||
render () { | ||
return ( | ||
<div id='on-click-page'> | ||
<Link href='/nav/on-click'> | ||
<a id='on-click-link' onClick={() => ++count}>Self Reload</a> | ||
</Link> | ||
<Link href='/nav/on-click'> | ||
<a id='on-click-link-prevent-default' onClick={(e) => { e.preventDefault(); ++count }}>Self Reload</a> | ||
</Link> | ||
<p>COUNT: {this.props.count}</p> | ||
</div> | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters