Skip to content

Commit 52cca98

Browse files
committed
[changed] Preventing transition from onClick
For consistency with React v0.14, don't prevent the transition if the onClick handler returns false.
1 parent 53d1c7e commit 52cca98

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

UPGRADE_GUIDE.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,7 @@ the link will not check if it's active.
139139

140140
Because named routes are gone, a link to `/` with an index route at `/`
141141
will always be active. So we've introduced `IndexLink` that is only
142-
active when the index route is active.
143-
144-
**Note:** `DefaultRoute` is gone.
142+
active when on exactly that path.
145143

146144
```js
147145
// v0.13.x
@@ -164,6 +162,12 @@ active when the index route is active.
164162
<IndexLink to="/">Home</IndexLink>
165163
```
166164

165+
#### onClick handler
166+
167+
For consistency with React v0.14, returning `false` from a `Link`'s `onClick`
168+
handler no longer prevents the transition. To prevent the transition, call
169+
`e.preventDefault()` instead.
170+
167171
### RouteHandler
168172

169173
`RouteHandler` is gone. `Router` now automatically populates

docs/API.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ The className a `<Link>` receives when its route is active. No active class by d
103103
The styles to apply to the link element when its route is active.
104104

105105
##### `onClick(e)`
106-
A custom handler for the click event. Works just like a handler on an `<a>` tag - calling `e.preventDefault()` or returning `false` will prevent the transition from firing, while `e.stopPropagation()` will prevent the event from bubbling.
106+
A custom handler for the click event. Works just like a handler on an `<a>` tag - calling `e.preventDefault()` will prevent the transition from firing, while `e.stopPropagation()` will prevent the event from bubbling.
107107

108108
##### *others*
109109
You can also pass props you'd like to be on the `<a>` such as a `title`, `id`, `className`, etc.

modules/Link.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ class Link extends Component {
6060
}
6161

6262
handleClick(event) {
63-
let allowTransition = true, clickResult
63+
let allowTransition = true
6464

6565
if (this.props.onClick)
66-
clickResult = this.props.onClick(event)
66+
this.props.onClick(event)
6767

6868
if (isModifiedEvent(event) || !isLeftClickEvent(event))
6969
return
7070

71-
if (clickResult === false || event.defaultPrevented === true)
71+
if (event.defaultPrevented === true)
7272
allowTransition = false
7373

7474
// If target prop is set (e.g. to "_blank") let browser handle link.

modules/__tests__/Link-test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,36 @@ describe('A <Link>', function () {
315315
</Router>
316316
), node, execNextStep)
317317
})
318+
319+
it('does not transition when onClick prevents default', function (done) {
320+
class LinkWrapper extends Component {
321+
render() {
322+
return <Link to="/hello" onClick={(e) => e.preventDefault()}>Link</Link>
323+
}
324+
}
325+
326+
const history = createHistory('/')
327+
const spy = spyOn(history, 'pushState').andCallThrough()
328+
329+
const steps = [
330+
function () {
331+
click(node.querySelector('a'), { button: 0 })
332+
},
333+
function () {
334+
expect(node.innerHTML).toMatch(/Link/)
335+
expect(spy).toNotHaveBeenCalled()
336+
}
337+
]
338+
339+
const execNextStep = execSteps(steps, done)
340+
341+
render((
342+
<Router history={history} onUpdate={execNextStep}>
343+
<Route path="/" component={LinkWrapper} />
344+
<Route path="/hello" component={Hello} />
345+
</Router>
346+
), node, execNextStep)
347+
})
318348
})
319349

320350
})

0 commit comments

Comments
 (0)