Skip to content

Commit 74ff6fb

Browse files
committed
Merge pull request #2039 from crm416/return-false
Deprecate 'return false' in event handlers
2 parents 95de877 + eb36b57 commit 74ff6fb

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

docs/docs/ref-05-events.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ Date timeStamp
2727
String type
2828
```
2929

30+
> Note:
31+
>
32+
> As of v0.12, returning `false` from an event handler will no longer stop event propagation. Instead, `e.stopPropagation()` or `e.preventDefault()` should be triggered manually, as appropriate.
33+
3034

3135
## Supported Events
3236

@@ -111,7 +115,7 @@ onDragOver onDragStart onDrop onMouseDown onMouseEnter onMouseLeave
111115
onMouseMove onMouseOut onMouseOver onMouseUp
112116
```
113117

114-
Properties:
118+
Properties:
115119

116120
```javascript
117121
boolean altKey

src/browser/__tests__/ReactBrowserEventEmitter-test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ describe('ReactBrowserEventEmitter', function() {
253253
expect(idCallOrder[0]).toBe(getID(CHILD));
254254
});
255255

256-
it('should stopPropagation if false is returned', function() {
256+
it('should stopPropagation if false is returned, but warn', function() {
257257
ReactBrowserEventEmitter.putListener(
258258
getID(CHILD),
259259
ON_CLICK_KEY,
@@ -269,9 +269,16 @@ describe('ReactBrowserEventEmitter', function() {
269269
ON_CLICK_KEY,
270270
recordID.bind(null, getID(GRANDPARENT))
271271
);
272+
spyOn(console, 'warn');
272273
ReactTestUtils.Simulate.click(CHILD);
273274
expect(idCallOrder.length).toBe(1);
274275
expect(idCallOrder[0]).toBe(getID(CHILD));
276+
expect(console.warn.calls.length).toEqual(1);
277+
expect(console.warn.calls[0].args[0]).toBe(
278+
'Warning: Returning `false` from an event handler is deprecated and ' +
279+
'will be ignored in a future release. Instead, manually call ' +
280+
'e.stopPropagation() or e.preventDefault(), as appropriate.'
281+
);
275282
});
276283

277284
/**

src/browser/eventPlugins/SimpleEventPlugin.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var getEventCharCode = require('getEventCharCode');
3535

3636
var invariant = require('invariant');
3737
var keyOf = require('keyOf');
38+
var warning = require('warning');
3839

3940
var topLevelTypes = EventConstants.topLevelTypes;
4041

@@ -301,14 +302,22 @@ var SimpleEventPlugin = {
301302

302303
/**
303304
* Same as the default implementation, except cancels the event when return
304-
* value is false.
305+
* value is false. This behavior will be disabled in a future release.
305306
*
306307
* @param {object} Event to be dispatched.
307308
* @param {function} Application-level callback.
308309
* @param {string} domID DOM ID to pass to the callback.
309310
*/
310311
executeDispatch: function(event, listener, domID) {
311312
var returnValue = EventPluginUtils.executeDispatch(event, listener, domID);
313+
314+
warning(
315+
typeof returnValue !== 'boolean',
316+
'Returning `false` from an event handler is deprecated and will be ' +
317+
'ignored in a future release. Instead, manually call ' +
318+
'e.stopPropagation() or e.preventDefault(), as appropriate.'
319+
);
320+
312321
if (returnValue === false) {
313322
event.stopPropagation();
314323
event.preventDefault();

0 commit comments

Comments
 (0)