mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Merge pull request #3628 from spicyj/do-not-bind
Kill ReactDoNotBindDeprecated
This commit is contained in:
@@ -485,13 +485,11 @@ function mixSpecIntoComponent(Constructor, spec) {
|
||||
var isReactClassMethod =
|
||||
ReactClassInterface.hasOwnProperty(name);
|
||||
var isAlreadyDefined = proto.hasOwnProperty(name);
|
||||
var markedDontBind = property && property.__reactDontBind;
|
||||
var isFunction = typeof property === 'function';
|
||||
var shouldAutoBind =
|
||||
isFunction &&
|
||||
!isReactClassMethod &&
|
||||
!isAlreadyDefined &&
|
||||
!markedDontBind;
|
||||
!isAlreadyDefined;
|
||||
|
||||
if (shouldAutoBind) {
|
||||
if (!proto.__reactAutoBindMap) {
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
/**
|
||||
* Copyright 2013-2015, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*
|
||||
* @providesModule ReactDoNotBindDeprecated
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
var ReactDoNotBindDeprecated = {
|
||||
/**
|
||||
* Marks the method for not being automatically bound on component mounting. A
|
||||
* couple of reasons you might want to use this:
|
||||
*
|
||||
* - Automatically supporting the previous behavior in components that were
|
||||
* built with previous versions of React.
|
||||
* - Tuning performance, by avoiding binding on initial render for methods
|
||||
* that are always invoked while being preceded by `this.`. Such binds are
|
||||
* unnecessary.
|
||||
*
|
||||
* React.createClass({
|
||||
* handleClick: ReactDoNotBindDeprecated.doNotBind(function() {
|
||||
* alert(this.setState); // undefined!
|
||||
* }),
|
||||
* render: function() {
|
||||
* return <a onClick={this.handleClick}>Jump</a>;
|
||||
* }
|
||||
* });
|
||||
*
|
||||
* @param {function} method Method to avoid automatically binding.
|
||||
* @public
|
||||
*/
|
||||
doNotBind: function(method) {
|
||||
method.__reactDontBind = true; // Mutating
|
||||
return method;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = ReactDoNotBindDeprecated;
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
var mocks = require('mocks');
|
||||
var React = require('React');
|
||||
var ReactDoNotBindDeprecated = require('ReactDoNotBindDeprecated');
|
||||
var ReactTestUtils = require('ReactTestUtils');
|
||||
var reactComponentExpect = require('reactComponentExpect');
|
||||
|
||||
@@ -30,8 +29,8 @@ describe('autobinding', function() {
|
||||
getInitialState: function() {
|
||||
return {something: 'hi'};
|
||||
},
|
||||
onMouseEnter: ReactDoNotBindDeprecated.doNotBind(mouseDidEnter),
|
||||
onMouseLeave: ReactDoNotBindDeprecated.doNotBind(mouseDidLeave),
|
||||
onMouseEnter: mouseDidEnter,
|
||||
onMouseLeave: mouseDidLeave,
|
||||
onClick: mouseDidClick,
|
||||
|
||||
// auto binding only occurs on top level functions in class defs.
|
||||
@@ -44,7 +43,7 @@ describe('autobinding', function() {
|
||||
render: function() {
|
||||
return (
|
||||
<div
|
||||
onMouseOver={this.onMouseEnter.bind(this)}
|
||||
onMouseOver={this.onMouseEnter}
|
||||
onMouseOut={this.onMouseLeave}
|
||||
onClick={this.onClick}
|
||||
/>
|
||||
@@ -69,8 +68,6 @@ describe('autobinding', function() {
|
||||
badIdea();
|
||||
}).toThrow();
|
||||
|
||||
expect(mountedInstance1.onMouseEnter).toBe(mountedInstance2.onMouseEnter);
|
||||
expect(mountedInstance1.onMouseLeave).toBe(mountedInstance2.onMouseLeave);
|
||||
expect(mountedInstance1.onClick).not.toBe(mountedInstance2.onClick);
|
||||
|
||||
ReactTestUtils.Simulate.click(rendered1);
|
||||
@@ -91,11 +88,11 @@ describe('autobinding', function() {
|
||||
|
||||
ReactTestUtils.Simulate.mouseOut(rendered1);
|
||||
expect(mouseDidLeave.mock.instances.length).toBe(1);
|
||||
expect(mouseDidLeave.mock.instances[0]).toBe(global);
|
||||
expect(mouseDidLeave.mock.instances[0]).toBe(mountedInstance1);
|
||||
|
||||
ReactTestUtils.Simulate.mouseOut(rendered2);
|
||||
expect(mouseDidLeave.mock.instances.length).toBe(2);
|
||||
expect(mouseDidLeave.mock.instances[1]).toBe(global);
|
||||
expect(mouseDidLeave.mock.instances[1]).toBe(mountedInstance2);
|
||||
});
|
||||
|
||||
it('works with mixins', function() {
|
||||
|
||||
@@ -15,7 +15,6 @@ var ChildUpdates;
|
||||
var MorphingComponent;
|
||||
var React;
|
||||
var ReactCurrentOwner;
|
||||
var ReactDoNotBindDeprecated;
|
||||
var ReactMount;
|
||||
var ReactPropTypes;
|
||||
var ReactServerRendering;
|
||||
@@ -33,7 +32,6 @@ describe('ReactCompositeComponent', function() {
|
||||
reactComponentExpect = require('reactComponentExpect');
|
||||
React = require('React');
|
||||
ReactCurrentOwner = require('ReactCurrentOwner');
|
||||
ReactDoNotBindDeprecated = require('ReactDoNotBindDeprecated');
|
||||
ReactPropTypes = require('ReactPropTypes');
|
||||
ReactTestUtils = require('ReactTestUtils');
|
||||
ReactMount = require('ReactMount');
|
||||
@@ -170,9 +168,6 @@ describe('ReactCompositeComponent', function() {
|
||||
methodAutoBound: function() {
|
||||
return this;
|
||||
},
|
||||
methodExplicitlyNotBound: ReactDoNotBindDeprecated.doNotBind(function() {
|
||||
return this;
|
||||
}),
|
||||
render: function() {
|
||||
return <div></div>;
|
||||
}
|
||||
@@ -189,9 +184,6 @@ describe('ReactCompositeComponent', function() {
|
||||
expect(function() {
|
||||
mountedInstance.methodAutoBound();
|
||||
}).not.toThrow();
|
||||
expect(function() {
|
||||
mountedInstance.methodExplicitlyNotBound();
|
||||
}).not.toThrow();
|
||||
|
||||
expect(console.error.argsForCall.length).toBe(1);
|
||||
var explicitlyBound = mountedInstance.methodToBeExplicitlyBound.bind(
|
||||
@@ -199,17 +191,13 @@ describe('ReactCompositeComponent', function() {
|
||||
);
|
||||
expect(console.error.argsForCall.length).toBe(2);
|
||||
var autoBound = mountedInstance.methodAutoBound;
|
||||
var explicitlyNotBound = mountedInstance.methodExplicitlyNotBound;
|
||||
|
||||
var context = {};
|
||||
expect(explicitlyBound.call(context)).toBe(mountedInstance);
|
||||
expect(autoBound.call(context)).toBe(mountedInstance);
|
||||
expect(explicitlyNotBound.call(context)).toBe(context);
|
||||
|
||||
expect(explicitlyBound.call(mountedInstance)).toBe(mountedInstance);
|
||||
expect(autoBound.call(mountedInstance)).toBe(mountedInstance);
|
||||
// This one is the weird one
|
||||
expect(explicitlyNotBound.call(mountedInstance)).toBe(mountedInstance);
|
||||
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user