MCPcopy Create free account
hub / github.com/reactjs/react-rails / bindAutoBindMethod

Function bindAutoBindMethod

lib/assets/react-source/development/react.js:29886–29937  ·  view source on GitHub ↗

* Binds a method to the component. * * @param {object} component Component whose method is going to be bound. * @param {function} method Method to be bound. * @return {function} The bound method.

(component, method)

Source from the content-addressed store, hash-verified

29884 * @return {function} The bound method.
29885 */
29886 function bindAutoBindMethod(component, method) {
29887 var boundMethod = method.bind(component);
29888 if (process.env.NODE_ENV !== 'production') {
29889 boundMethod.__reactBoundContext = component;
29890 boundMethod.__reactBoundMethod = method;
29891 boundMethod.__reactBoundArguments = null;
29892 var componentName = component.constructor.displayName;
29893 var _bind = boundMethod.bind;
29894 boundMethod.bind = function(newThis) {
29895 for (
29896 var _len = arguments.length,
29897 args = Array(_len > 1 ? _len - 1 : 0),
29898 _key = 1;
29899 _key < _len;
29900 _key++
29901 ) {
29902 args[_key - 1] = arguments[_key];
29903 }
29904
29905 // User is trying to bind() an autobound method; we effectively will
29906 // ignore the value of "this" that the user is trying to use, so
29907 // let's warn.
29908 if (newThis !== component && newThis !== null) {
29909 if (process.env.NODE_ENV !== 'production') {
29910 warning(
29911 false,
29912 'bind(): React component methods may only be bound to the ' +
29913 'component instance. See %s',
29914 componentName
29915 );
29916 }
29917 } else if (!args.length) {
29918 if (process.env.NODE_ENV !== 'production') {
29919 warning(
29920 false,
29921 'bind(): You are binding a component method to the component. ' +
29922 'React does this for you automatically in a high-performance ' +
29923 'way, so you can safely remove this call. See %s',
29924 componentName
29925 );
29926 }
29927 return boundMethod;
29928 }
29929 var reboundMethod = _bind.apply(boundMethod, arguments);
29930 reboundMethod.__reactBoundContext = component;
29931 reboundMethod.__reactBoundMethod = method;
29932 reboundMethod.__reactBoundArguments = args;
29933 return reboundMethod;
29934 };
29935 }
29936 return boundMethod;
29937 }
29938
29939 /**
29940 * Binds all auto-bound methods in a component.

Callers 1

bindAutoBindMethodsFunction · 0.70

Calls 1

warningFunction · 0.70

Tested by

no test coverage detected