mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Hardened validateCallback to better handle null values (#8973)
Previously, calls to validateCallback() with a null callback value resulted in runtime errors if a certain transform was not applied prior to running. This commit wraps the invariant() with the condition check so as to avoid calling formatUnexpectedArgument() unless necessary. It also replaces the truthy/falsy callback check with an explicit check for improved performance.
This commit is contained in:
@@ -28,13 +28,19 @@ function formatUnexpectedArgument(arg: any) {
|
||||
}
|
||||
|
||||
function validateCallback(callback: ?Function, callerName: string) {
|
||||
invariant(
|
||||
!callback || typeof callback === 'function',
|
||||
'%s(...): Expected the last optional `callback` argument to be a ' +
|
||||
'function. Instead received: %s.',
|
||||
callerName,
|
||||
formatUnexpectedArgument(callback)
|
||||
);
|
||||
if (
|
||||
callback !== null &&
|
||||
callback !== undefined &&
|
||||
typeof callback !== 'function'
|
||||
) {
|
||||
invariant(
|
||||
false,
|
||||
'%s(...): Expected the last optional `callback` argument to be a ' +
|
||||
'function. Instead received: %s.',
|
||||
callerName,
|
||||
formatUnexpectedArgument(callback)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = validateCallback;
|
||||
|
||||
Reference in New Issue
Block a user