-
Notifications
You must be signed in to change notification settings - Fork 47.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Warn early for non-functional event listeners (#10453)
* Add early warning for non-functional event listeners * Use functional listeners in ReactDOMComponent test To avoid triggering the non-functional event listener component * spy on console.error in non-function EventPluginHub test This should warn from ReactDOMFiberComponent, but we just ignore it here * Remove redundant check for listener * Add expectation for non-functional listener warning in EventPluginHub * Hoist listener typeof check in hot paths * Include stack addendum in non-functional listener warning * Make it pretty * Remove it.onnly from ReactDOMFiber test * Fix message * Update expected message * Change invariant message to match the new style * Fix remaining warning
- Loading branch information
Showing
5 changed files
with
57 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ var setTextContent = require('setTextContent'); | |
|
||
if (__DEV__) { | ||
var warning = require('fbjs/lib/warning'); | ||
var {getCurrentFiberStackAddendum} = require('ReactDebugCurrentFiber'); | ||
var ReactDOMInvalidARIAHook = require('ReactDOMInvalidARIAHook'); | ||
var ReactDOMNullInputValuePropHook = require('ReactDOMNullInputValuePropHook'); | ||
var ReactDOMUnknownPropertyHook = require('ReactDOMUnknownPropertyHook'); | ||
|
@@ -118,6 +119,16 @@ if (__DEV__) { | |
warning(false, 'Extra attributes from the server: %s', names); | ||
}; | ||
|
||
var warnForInvalidEventListener = function(registrationName, listener) { | ||
warning( | ||
false, | ||
'Expected `%s` listener to be a function, instead got a value of `%s` type.%s', | ||
registrationName, | ||
typeof listener, | ||
getCurrentFiberStackAddendum(), | ||
); | ||
}; | ||
|
||
var testDocument; | ||
// Parse the HTML and read it back to normalize the HTML string so that it | ||
// can be used for comparison. | ||
|
@@ -223,6 +234,9 @@ function setInitialDOMProperties( | |
// Noop | ||
} else if (registrationNameModules.hasOwnProperty(propKey)) { | ||
if (nextProp) { | ||
if (__DEV__ && typeof nextProp !== 'function') { | ||
warnForInvalidEventListener(propKey, nextProp); | ||
} | ||
ensureListeningTo(rootContainerElement, propKey); | ||
} | ||
} else if (isCustomComponentTag) { | ||
|
@@ -695,6 +709,9 @@ var ReactDOMFiberComponent = { | |
} else if (registrationNameModules.hasOwnProperty(propKey)) { | ||
if (nextProp) { | ||
// We eagerly listen to this even though we haven't committed yet. | ||
if (__DEV__ && typeof nextProp !== 'function') { | ||
warnForInvalidEventListener(propKey, nextProp); | ||
} | ||
ensureListeningTo(rootContainerElement, propKey); | ||
} | ||
if (!updatePayload && lastProp !== nextProp) { | ||
|
@@ -934,6 +951,9 @@ var ReactDOMFiberComponent = { | |
} | ||
} | ||
} else if (registrationNameModules.hasOwnProperty(propKey)) { | ||
if (__DEV__ && typeof nextProp !== 'function') { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
aweary
Author
Contributor
|
||
warnForInvalidEventListener(propKey, nextProp); | ||
} | ||
if (nextProp) { | ||
ensureListeningTo(rootContainerElement, propKey); | ||
} | ||
|
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
Just notice this should probably be inside
if (nextProp)
, no? Otherwise it will "catch"null
andundefined
which are valid values. I think this is a bug. @aweary What do you think?Also ideally we should check
if (nextProp !== null && typeof nextProp !== 'undefined')
rather than justif (nextProp)
? Although that's a separate issue.