@samitouri / QOS-React-2 / commits / 92dab0759c
Fork 0

The library for web and native user interfaces.

More autobind warnings and invariants

This adds two new warnings and one new invariant: - Warn when using React.autoBind() that it is deprecated. - Throw when calling bind() on an autobound method with the wrong value of "this". Today we'll silently ignore the provided value, which is confusing. - Warn when calling bind() on an autobound method with the *right* value of "this" and no other arguments. This is already done for you by React.

Pete Hunt committed Jul 10, 2013 at 14:44 UTC 92dab0759c7479f075bbe998cc21ddb3af51f15b
1 file changed +32 -2
src/core/ReactCompositeComponent.js
+32 -2
@@ -809,11 +809,34 @@ var ReactCompositeComponentMixin = {
809 */
810 _bindAutoBindMethod: function(method) {
811 var component = this;
812 - return function() {
812 + var boundMethod = function() {
813 return method.apply(component, arguments);
814 };
815 + if (__DEV__) {
816 + var componentName = component.constructor.displayName;
817 + var _bind = boundMethod.bind;
818 + boundMethod.bind = function(newThis) {
819 + // User is trying to bind() an autobound method; we effectively will
820 + // ignore the value of "this" that the user is trying to use, so
821 + // let's warn.
822 + if (newThis !== component) {
823 + console.warn(
824 + 'bind(): React component methods may only be bound to the ' +
825 + 'component instance. See ' + componentName
826 + );
827 + } else if (arguments.length === 1) {
828 + console.warn(
829 + 'bind(): You are binding a component method to the component. ' +
830 + 'React does this for you automatically in a high-performance ' +
831 + 'way, so you can safely remove this call. See ' + componentName
832 + );
833 + return boundMethod;
834 + }
835 + return _bind.apply(boundMethod, arguments);
836 + };
837 + }
838 + return boundMethod;
839 }
816 -
840 };
841
842 var ReactCompositeComponentBase = function() {};
@@ -872,6 +895,13 @@ var ReactCompositeComponent = {
895 * @public
896 */
897 autoBind: function(method) {
898 + if (__DEV__) {
899 + console.warn && console.warn(
900 + 'React.autoBind() is now deprecated. All React component methods ' +
901 + 'are auto bound by default, so React.autoBind() is a no-op. It ' +
902 + 'will be removed in the next version of React'
903 + );
904 + }
905 return method;
906 }
907 };