Skip to content

Deprecate 'return false' in event handlers #2039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 27, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/docs/ref-05-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ Date timeStamp
String type
```

> Note:
>
> 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.


## Supported Events

Expand Down Expand Up @@ -111,7 +115,7 @@ onDragOver onDragStart onDrop onMouseDown onMouseEnter onMouseLeave
onMouseMove onMouseOut onMouseOver onMouseUp
```

Properties:
Properties:

```javascript
boolean altKey
Expand Down
9 changes: 8 additions & 1 deletion src/browser/__tests__/ReactBrowserEventEmitter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ describe('ReactBrowserEventEmitter', function() {
expect(idCallOrder[0]).toBe(getID(CHILD));
});

it('should stopPropagation if false is returned', function() {
it('should stopPropagation if false is returned, but warn', function() {
ReactBrowserEventEmitter.putListener(
getID(CHILD),
ON_CLICK_KEY,
Expand All @@ -269,9 +269,16 @@ describe('ReactBrowserEventEmitter', function() {
ON_CLICK_KEY,
recordID.bind(null, getID(GRANDPARENT))
);
spyOn(console, 'warn');
ReactTestUtils.Simulate.click(CHILD);
expect(idCallOrder.length).toBe(1);
expect(idCallOrder[0]).toBe(getID(CHILD));
expect(console.warn.calls.length).toEqual(1);
expect(console.warn.calls[0].args[0]).toBe(
'Warning: Returning `false` from an event handler is deprecated and ' +
'will be ignored in a future release. Instead, manually call ' +
'e.stopPropagation() or e.preventDefault(), as appropriate.'
);
});

/**
Expand Down
11 changes: 10 additions & 1 deletion src/browser/eventPlugins/SimpleEventPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var getEventCharCode = require('getEventCharCode');

var invariant = require('invariant');
var keyOf = require('keyOf');
var warning = require('warning');

var topLevelTypes = EventConstants.topLevelTypes;

Expand Down Expand Up @@ -301,14 +302,22 @@ var SimpleEventPlugin = {

/**
* Same as the default implementation, except cancels the event when return
* value is false.
* value is false. This behavior will be disabled in a future release.
*
* @param {object} Event to be dispatched.
* @param {function} Application-level callback.
* @param {string} domID DOM ID to pass to the callback.
*/
executeDispatch: function(event, listener, domID) {
var returnValue = EventPluginUtils.executeDispatch(event, listener, domID);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of removing this completely now, we should make a release where this still works but it gives a warning (in __DEV__) using console.warn – if you search around you should see other examples of this. It's also good to add a monitorCodeUse call which does nothing in the open-source build by default but makes a log internally at Facebook (and theoretically outside as well for sophisticated clients – I've been meaning to set it up at KA but haven't had a chance yet).

After people have had a chance to upgrade, we can drop it completely.


warning(
typeof returnValue !== 'boolean',
'Returning `false` from an event handler is deprecated and will be ' +
'ignored in a future release. Instead, manually call ' +
'e.stopPropagation() or e.preventDefault(), as appropriate.'
);

if (returnValue === false) {
event.stopPropagation();
event.preventDefault();
Expand Down