Remove ReactStateSetters (#9107)

This commit is contained in:
Brandon Dail 2017-03-03 22:42:25 -06:00 committed by GitHub
parent d87c4d5e5c
commit ceafdbf0a5
3 changed files with 0 additions and 264 deletions

View File

@ -784,14 +784,6 @@ src/renderers/__tests__/ReactPerf-test.js
* should not print errant warnings if portal throws in componentWillMount()
* should not print errant warnings if portal throws in componentDidMount()
src/renderers/__tests__/ReactStateSetters-test.js
* createStateSetter should update state
* createStateKeySetter should update state
* createStateKeySetter is memoized
* createStateSetter should update state from mixin
* createStateKeySetter should update state with mixin
* createStateKeySetter is memoized with mixin
src/renderers/__tests__/ReactStatelessComponent-test.js
* should render stateless component
* should update stateless component

View File

@ -1,104 +0,0 @@
/**
* Copyright 2013-present, 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 ReactStateSetters
*/
'use strict';
var ReactStateSetters = {
/**
* Returns a function that calls the provided function, and uses the result
* of that to set the component's state.
*
* @param {ReactCompositeComponent} component
* @param {function} funcReturningState Returned callback uses this to
* determine how to update state.
* @return {function} callback that when invoked uses funcReturningState to
* determined the object literal to setState.
*/
createStateSetter: function(component, funcReturningState) {
return function(a, b, c, d, e, f) {
var partialState = funcReturningState.call(component, a, b, c, d, e, f);
if (partialState) {
component.setState(partialState);
}
};
},
/**
* Returns a single-argument callback that can be used to update a single
* key in the component's state.
*
* Note: this is memoized function, which makes it inexpensive to call.
*
* @param {ReactCompositeComponent} component
* @param {string} key The key in the state that you should update.
* @return {function} callback of 1 argument which calls setState() with
* the provided keyName and callback argument.
*/
createStateKeySetter: function(component, key) {
// Memoize the setters.
var cache = component.__keySetters || (component.__keySetters = {});
return cache[key] || (cache[key] = createStateKeySetter(component, key));
},
};
function createStateKeySetter(component, key) {
// Partial state is allocated outside of the function closure so it can be
// reused with every call, avoiding memory allocation when this function
// is called.
var partialState = {};
return function stateKeySetter(value) {
partialState[key] = value;
component.setState(partialState);
};
}
ReactStateSetters.Mixin = {
/**
* Returns a function that calls the provided function, and uses the result
* of that to set the component's state.
*
* For example, these statements are equivalent:
*
* this.setState({x: 1});
* this.createStateSetter(function(xValue) {
* return {x: xValue};
* })(1);
*
* @param {function} funcReturningState Returned callback uses this to
* determine how to update state.
* @return {function} callback that when invoked uses funcReturningState to
* determined the object literal to setState.
*/
createStateSetter: function(funcReturningState) {
return ReactStateSetters.createStateSetter(this, funcReturningState);
},
/**
* Returns a single-argument callback that can be used to update a single
* key in the component's state.
*
* For example, these statements are equivalent:
*
* this.setState({x: 1});
* this.createStateKeySetter('x')(1);
*
* Note: this is memoized function, which makes it inexpensive to call.
*
* @param {string} key The key in the state that you should update.
* @return {function} callback of 1 argument which calls setState() with
* the provided keyName and callback argument.
*/
createStateKeySetter: function(key) {
return ReactStateSetters.createStateKeySetter(this, key);
},
};
module.exports = ReactStateSetters;

View File

@ -1,152 +0,0 @@
/**
* Copyright 2013-present, 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.
*
* @emails react-core
*/
'use strict';
var React = require('react');
var ReactStateSetters = require('ReactStateSetters');
var ReactTestUtils = require('ReactTestUtils');
var TestComponent;
var TestComponentWithMixin;
describe('ReactStateSetters', () => {
beforeEach(() => {
jest.resetModules();
TestComponent = class extends React.Component {
state = {foo: 'foo'};
render() {
return <div />;
}
};
TestComponentWithMixin = React.createClass({
mixins: [ReactStateSetters.Mixin],
getInitialState: function() {
return {foo: 'foo'};
},
render: function() {
return <div />;
},
});
});
it('createStateSetter should update state', () => {
var instance = <TestComponent />;
instance = ReactTestUtils.renderIntoDocument(instance);
expect(instance.state).toEqual({foo: 'foo'});
var setter = ReactStateSetters.createStateSetter(
instance,
function(a, b, c) {
return {
foo: a + b + c,
bar: a * b * c,
};
}
);
expect(instance.state).toEqual({foo: 'foo'});
setter(1, 2, 3);
expect(instance.state).toEqual({foo: 6, bar: 6});
setter(10, 11, 12);
expect(instance.state).toEqual({foo: 33, bar: 1320});
});
it('createStateKeySetter should update state', () => {
var instance = <TestComponent />;
instance = ReactTestUtils.renderIntoDocument(instance);
expect(instance.state).toEqual({foo: 'foo'});
var setter = ReactStateSetters.createStateKeySetter(instance, 'foo');
expect(instance.state).toEqual({foo: 'foo'});
setter('bar');
expect(instance.state).toEqual({foo: 'bar'});
setter('baz');
expect(instance.state).toEqual({foo: 'baz'});
});
it('createStateKeySetter is memoized', () => {
var instance = <TestComponent />;
instance = ReactTestUtils.renderIntoDocument(instance);
expect(instance.state).toEqual({foo: 'foo'});
var foo1 = ReactStateSetters.createStateKeySetter(instance, 'foo');
var bar1 = ReactStateSetters.createStateKeySetter(instance, 'bar');
var foo2 = ReactStateSetters.createStateKeySetter(instance, 'foo');
var bar2 = ReactStateSetters.createStateKeySetter(instance, 'bar');
expect(foo2).toBe(foo1);
expect(bar2).toBe(bar1);
});
it('createStateSetter should update state from mixin', () => {
var instance = <TestComponentWithMixin />;
instance = ReactTestUtils.renderIntoDocument(instance);
expect(instance.state).toEqual({foo: 'foo'});
var setter = instance.createStateSetter(
function(a, b, c) {
return {
foo: a + b + c,
bar: a * b * c,
};
}
);
expect(instance.state).toEqual({foo: 'foo'});
setter(1, 2, 3);
expect(instance.state).toEqual({foo: 6, bar: 6});
setter(10, 11, 12);
expect(instance.state).toEqual({foo: 33, bar: 1320});
});
it('createStateKeySetter should update state with mixin', () => {
var instance = <TestComponentWithMixin />;
instance = ReactTestUtils.renderIntoDocument(instance);
expect(instance.state).toEqual({foo: 'foo'});
var setter = instance.createStateKeySetter('foo');
expect(instance.state).toEqual({foo: 'foo'});
setter('bar');
expect(instance.state).toEqual({foo: 'bar'});
setter('baz');
expect(instance.state).toEqual({foo: 'baz'});
});
it('createStateKeySetter is memoized with mixin', () => {
var instance = <TestComponentWithMixin />;
instance = ReactTestUtils.renderIntoDocument(instance);
expect(instance.state).toEqual({foo: 'foo'});
var foo1 = instance.createStateKeySetter('foo');
var bar1 = instance.createStateKeySetter('bar');
var foo2 = instance.createStateKeySetter('foo');
var bar2 = instance.createStateKeySetter('bar');
expect(foo2).toBe(foo1);
expect(bar2).toBe(bar1);
});
});