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

Deprecate 'return false' in event handlers #2039

merged 9 commits into from
Aug 27, 2014

Conversation

charliermarsh
Copy link

Fixes #2015. Wasn't sure how to handle react-version in _config.yml, given that this is a v0.12 milestone.

@@ -34,4 +34,4 @@ sass:
sass_dir: _css
gems:
- jekyll-redirect-from
react_version: 0.11.1
react_version: 0.12.0-alpha
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just leave this alone – docs are built from the 0.11-stable branch and we just won't cherry-pick this in.

* @param {function} Application-level callback.
* @param {string} domID DOM ID to pass to the callback.
*/
/**
Copy link
Collaborator

Choose a reason for hiding this comment

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

the indentation here looks messed up?

@charliermarsh
Copy link
Author

@spicyj @zpao Fixed! Thanks.

if (__DEV__) {
monitorCodeUse('react_event_return_false');
console.warn(
'Returning `false` from an event handler will be deprecated in a ' +
Copy link
Collaborator

Choose a reason for hiding this comment

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

one more tweak (sorry): let's say "is deprecated and will be ignored in a future release."

Copy link
Contributor

Choose a reason for hiding this comment

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

Should it also include === true? For handler() {return bla.shouldReturnFalse;}

Copy link
Author

Choose a reason for hiding this comment

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

Seems reasonable to me.

@charliermarsh
Copy link
Author

@spicyj @chenglou Updated again.

@@ -309,6 +310,17 @@ var SimpleEventPlugin = {
*/
executeDispatch: function(event, listener, domID) {
var returnValue = EventPluginUtils.executeDispatch(event, listener, domID);
if (__DEV__) {
if (typeof returnValue === 'boolean') {
monitorCodeUse('react_event_return_false');
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, missed this: internally, this is a different function that logs the message. You'd pass a second parameter, which is an object that contains all you need to know to trace back to this function so that people can act upon it:

monitorCodeUse('react_event_return_false', {eventHandlerName: listener.__reactBoundMethod.displayName});

And maybe some other info, dunno. See #2052

Copy link
Collaborator

Choose a reason for hiding this comment

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

FWIW we already have these in the code:

monitorCodeUse('react_object_map_children');
monitorCodeUse('react_no_scroll_event');

I guess the call stack isn't particularly useful though in this case.

Copy link
Contributor

Choose a reason for hiding this comment

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

Right, and we don't actually bother tracking these two down. These are only to give an idea of the usage pattern internally.

@charliermarsh
Copy link
Author

@chenglou Fixed. (Note that this now depends on #2052, so the tests won't pass until that's merged in--is there a good way to indicate that?)

@chenglou
Copy link
Contributor

Nope. Ill just fix that PR and pull it in quick.

if (__DEV__) {
if (typeof returnValue === 'boolean') {
monitorCodeUse('react_event_return_false', {
eventHandlerName: listener.__reactBoundMethod.displayName
Copy link
Contributor

Choose a reason for hiding this comment

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

@crm416 ugh sorry, I derped. This is better as (listener.__reactBoundContext && listener.__reactBoundContext.constructor.displayName) || '<<anonymous>>'. It makes more sense to go find the displayName this way, than to look for it on the handler, since the handler can come from a mixin (in which case the method's displayName is kinda useless no matter how we do it).

So if you change this line to that, it won't depend on the other PR anymore, and I can pull this in.

@charliermarsh
Copy link
Author

@chenglou No problem. Should be fixed now.

if (typeof returnValue === 'boolean') {
var eventHandlerName = (listener.__reactBoundContext &&
listener.__reactBoundContext.constructor.displayName) ||
'<<anonymous>>';
Copy link
Member

Choose a reason for hiding this comment

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

this assignment is really tough to read, and I think is better written with a ternary

        var eventHandlerName = listener.__reactBoundContext ?
          listener.__reactBoundContext.constructor.displayName :
          '<<anonymous>>';

Copy link
Member

Choose a reason for hiding this comment

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

Though I suppose that capture the case where listener.__reactBoundContext.constructor.displayName is undefined...

        var eventHandlerName =
          (listener.__reactBoundContext &&
           listener.__reactBoundContext.constructor.displayName) ||
          '<<anonymous>>';

is slightly better I suppose...

Copy link
Contributor

Choose a reason for hiding this comment

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

My bad. Also, would a component name be enough? I guess it is?

@charliermarsh
Copy link
Author

@zpao Fixed! Thanks.

@chenglou
Copy link
Contributor

@crm416: @sebmarkbage has spoken. You can take out that monitorCodeUse completely; I just tested internally and the important things don't break, so we probably don't need to track that anymore. Sorry for the back and forth!

@charliermarsh
Copy link
Author

@chenglou No worries--monitorCodeUse removed.

@@ -309,6 +309,16 @@ var SimpleEventPlugin = {
*/
executeDispatch: function(event, listener, domID) {
var returnValue = EventPluginUtils.executeDispatch(event, listener, domID);
if (__DEV__) {
if (typeof returnValue === 'boolean') {
Copy link
Contributor

Choose a reason for hiding this comment

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

There's a warning module (similar to invariant. Use that one instead.

@charliermarsh
Copy link
Author

I noticed that in ReactPropTransferer, we have a didWarn flag to avoid printing the "transferPropsTo is deprecated" warning excessively. Seems like that would be useful here too?

@chenglou
Copy link
Contributor

@sebmarkbage ^
I think this should be ok? It's kinda hard to track and there's apparently no important users of return false anyways.

chenglou added a commit that referenced this pull request Aug 27, 2014
Deprecate 'return false' in event handlers
@chenglou chenglou merged commit 74ff6fb into facebook:master Aug 27, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Deprecate "return false" in event handlers
4 participants