Skip to content

Commit

Permalink
Warning about components implementing shouldComponentUpdate()
Browse files Browse the repository at this point in the history
instead of throwing an error.
  • Loading branch information
felixgirault committed Oct 28, 2016
1 parent 8e349b7 commit 28488ef
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions index.js
Expand Up @@ -4,8 +4,11 @@
*/
'use strict';

var warning = require('fbjs/lib/warning');
var shallowEqual = require('fbjs/lib/shallowEqual');



/**
* Tells if a component should update given it's next props
* and state.
Expand All @@ -18,18 +21,22 @@ function shouldComponentUpdate(nextProps, nextState) {
}

/**
* Get a text description of the component that can be used to identify it
* in error messages.
* Returns a text description of the component that can be
* used to identify it in error messages.
*
* Adapted from: https://github.com/facebook/react/blob/v15.4.0-rc.3/src/renderers/shared/stack/reconciler/ReactCompositeComponent.js#L1143
* @return {string} The name or "a component".
* @see https://github.com/facebook/react/blob/v15.4.0-rc.3/src/renderers/shared/stack/reconciler/ReactCompositeComponent.js#L1143
* @param {function} component The component.
* @return {string} The name of the component.
*/
function getComponentName(component) {
var constructor = component.prototype && component.prototype.constructor;

return (
component.displayName || (constructor && constructor.displayName) ||
component.name || (constructor && constructor.name) ||
"a component"
component.displayName
|| (constructor && constructor.displayName)
|| component.name
|| (constructor && constructor.name)
|| 'a component'
);
}

Expand All @@ -40,10 +47,16 @@ function getComponentName(component) {
*/
function pureRenderDecorator(component) {
if (component.prototype.shouldComponentUpdate !== undefined) {
var componentName = getComponentName(component);
throw new Error("Cannot add a pure render decorator to " + componentName
+ ", because it already implements `shouldComponentUpdate()`.")
// We're not using the condition mecanism of warning()
// here to avoid useless calls to getComponentName().
warning(
false,
'Cannot decorate `%s` with @pureRenderDecorator, '
+ 'because it already implements `shouldComponentUpdate().',
getComponentName(component)
)
}

component.prototype.shouldComponentUpdate = shouldComponentUpdate;
return component;
}
Expand Down

0 comments on commit 28488ef

Please sign in to comment.