@samitouri / QOS-React / commits / ceafdbf0a5

Remove ReactStateSetters (#9107)

Brandon Dail committed Mar 3, 2017 at 22:42 UTC ceafdbf0a544556fbf210ff52dee47b765bf0f0a
3 files changed -264
scripts/fiber/tests-passing.txt
-8
@@ -784,14 +784,6 @@ src/renderers/__tests__/ReactPerf-test.js
784 * should not print errant warnings if portal throws in componentWillMount()
785 * should not print errant warnings if portal throws in componentDidMount()
786
787 -src/renderers/__tests__/ReactStateSetters-test.js
788 -* createStateSetter should update state
789 -* createStateKeySetter should update state
790 -* createStateKeySetter is memoized
791 -* createStateSetter should update state from mixin
792 -* createStateKeySetter should update state with mixin
793 -* createStateKeySetter is memoized with mixin
794 -
787 src/renderers/__tests__/ReactStatelessComponent-test.js
788 * should render stateless component
789 * should update stateless component
src/addons/link/ReactStateSetters.js deleted
-104
@@ -1,104 +0,0 @@
1 -/**
2 - * Copyright 2013-present, Facebook, Inc.
3 - * All rights reserved.
4 - *
5 - * This source code is licensed under the BSD-style license found in the
6 - * LICENSE file in the root directory of this source tree. An additional grant
7 - * of patent rights can be found in the PATENTS file in the same directory.
8 - *
9 - * @providesModule ReactStateSetters
10 - */
11 -
12 -'use strict';
13 -
14 -var ReactStateSetters = {
15 - /**
16 - * Returns a function that calls the provided function, and uses the result
17 - * of that to set the component's state.
18 - *
19 - * @param {ReactCompositeComponent} component
20 - * @param {function} funcReturningState Returned callback uses this to
21 - * determine how to update state.
22 - * @return {function} callback that when invoked uses funcReturningState to
23 - * determined the object literal to setState.
24 - */
25 - createStateSetter: function(component, funcReturningState) {
26 - return function(a, b, c, d, e, f) {
27 - var partialState = funcReturningState.call(component, a, b, c, d, e, f);
28 - if (partialState) {
29 - component.setState(partialState);
30 - }
31 - };
32 - },
33 -
34 - /**
35 - * Returns a single-argument callback that can be used to update a single
36 - * key in the component's state.
37 - *
38 - * Note: this is memoized function, which makes it inexpensive to call.
39 - *
40 - * @param {ReactCompositeComponent} component
41 - * @param {string} key The key in the state that you should update.
42 - * @return {function} callback of 1 argument which calls setState() with
43 - * the provided keyName and callback argument.
44 - */
45 - createStateKeySetter: function(component, key) {
46 - // Memoize the setters.
47 - var cache = component.__keySetters || (component.__keySetters = {});
48 - return cache[key] || (cache[key] = createStateKeySetter(component, key));
49 - },
50 -};
51 -
52 -function createStateKeySetter(component, key) {
53 - // Partial state is allocated outside of the function closure so it can be
54 - // reused with every call, avoiding memory allocation when this function
55 - // is called.
56 - var partialState = {};
57 - return function stateKeySetter(value) {
58 - partialState[key] = value;
59 - component.setState(partialState);
60 - };
61 -}
62 -
63 -ReactStateSetters.Mixin = {
64 - /**
65 - * Returns a function that calls the provided function, and uses the result
66 - * of that to set the component's state.
67 - *
68 - * For example, these statements are equivalent:
69 - *
70 - * this.setState({x: 1});
71 - * this.createStateSetter(function(xValue) {
72 - * return {x: xValue};
73 - * })(1);
74 - *
75 - * @param {function} funcReturningState Returned callback uses this to
76 - * determine how to update state.
77 - * @return {function} callback that when invoked uses funcReturningState to
78 - * determined the object literal to setState.
79 - */
80 - createStateSetter: function(funcReturningState) {
81 - return ReactStateSetters.createStateSetter(this, funcReturningState);
82 - },
83 -
84 - /**
85 - * Returns a single-argument callback that can be used to update a single
86 - * key in the component's state.
87 - *
88 - * For example, these statements are equivalent:
89 - *
90 - * this.setState({x: 1});
91 - * this.createStateKeySetter('x')(1);
92 - *
93 - * Note: this is memoized function, which makes it inexpensive to call.
94 - *
95 - * @param {string} key The key in the state that you should update.
96 - * @return {function} callback of 1 argument which calls setState() with
97 - * the provided keyName and callback argument.
98 - */
99 - createStateKeySetter: function(key) {
100 - return ReactStateSetters.createStateKeySetter(this, key);
101 - },
102 -};
103 -
104 -module.exports = ReactStateSetters;
src/renderers/__tests__/ReactStateSetters-test.js deleted
-152
@@ -1,152 +0,0 @@
1 -/**
2 - * Copyright 2013-present, Facebook, Inc.
3 - * All rights reserved.
4 - *
5 - * This source code is licensed under the BSD-style license found in the
6 - * LICENSE file in the root directory of this source tree. An additional grant
7 - * of patent rights can be found in the PATENTS file in the same directory.
8 - *
9 - * @emails react-core
10 - */
11 -
12 -'use strict';
13 -
14 -var React = require('react');
15 -var ReactStateSetters = require('ReactStateSetters');
16 -var ReactTestUtils = require('ReactTestUtils');
17 -
18 -var TestComponent;
19 -var TestComponentWithMixin;
20 -
21 -describe('ReactStateSetters', () => {
22 - beforeEach(() => {
23 - jest.resetModules();
24 -
25 - TestComponent = class extends React.Component {
26 - state = {foo: 'foo'};
27 -
28 - render() {
29 - return <div />;
30 - }
31 - };
32 -
33 - TestComponentWithMixin = React.createClass({
34 - mixins: [ReactStateSetters.Mixin],
35 -
36 - getInitialState: function() {
37 - return {foo: 'foo'};
38 - },
39 -
40 - render: function() {
41 - return <div />;
42 - },
43 - });
44 - });
45 -
46 - it('createStateSetter should update state', () => {
47 - var instance = <TestComponent />;
48 - instance = ReactTestUtils.renderIntoDocument(instance);
49 - expect(instance.state).toEqual({foo: 'foo'});
50 -
51 - var setter = ReactStateSetters.createStateSetter(
52 - instance,
53 - function(a, b, c) {
54 - return {
55 - foo: a + b + c,
56 - bar: a * b * c,
57 - };
58 - }
59 - );
60 - expect(instance.state).toEqual({foo: 'foo'});
61 -
62 - setter(1, 2, 3);
63 - expect(instance.state).toEqual({foo: 6, bar: 6});
64 -
65 - setter(10, 11, 12);
66 - expect(instance.state).toEqual({foo: 33, bar: 1320});
67 - });
68 -
69 - it('createStateKeySetter should update state', () => {
70 - var instance = <TestComponent />;
71 - instance = ReactTestUtils.renderIntoDocument(instance);
72 - expect(instance.state).toEqual({foo: 'foo'});
73 -
74 - var setter = ReactStateSetters.createStateKeySetter(instance, 'foo');
75 -
76 - expect(instance.state).toEqual({foo: 'foo'});
77 -
78 - setter('bar');
79 - expect(instance.state).toEqual({foo: 'bar'});
80 -
81 - setter('baz');
82 - expect(instance.state).toEqual({foo: 'baz'});
83 - });
84 -
85 - it('createStateKeySetter is memoized', () => {
86 - var instance = <TestComponent />;
87 - instance = ReactTestUtils.renderIntoDocument(instance);
88 - expect(instance.state).toEqual({foo: 'foo'});
89 -
90 - var foo1 = ReactStateSetters.createStateKeySetter(instance, 'foo');
91 - var bar1 = ReactStateSetters.createStateKeySetter(instance, 'bar');
92 -
93 - var foo2 = ReactStateSetters.createStateKeySetter(instance, 'foo');
94 - var bar2 = ReactStateSetters.createStateKeySetter(instance, 'bar');
95 -
96 - expect(foo2).toBe(foo1);
97 - expect(bar2).toBe(bar1);
98 - });
99 -
100 - it('createStateSetter should update state from mixin', () => {
101 - var instance = <TestComponentWithMixin />;
102 - instance = ReactTestUtils.renderIntoDocument(instance);
103 - expect(instance.state).toEqual({foo: 'foo'});
104 -
105 - var setter = instance.createStateSetter(
106 - function(a, b, c) {
107 - return {
108 - foo: a + b + c,
109 - bar: a * b * c,
110 - };
111 - }
112 - );
113 - expect(instance.state).toEqual({foo: 'foo'});
114 -
115 - setter(1, 2, 3);
116 - expect(instance.state).toEqual({foo: 6, bar: 6});
117 -
118 - setter(10, 11, 12);
119 - expect(instance.state).toEqual({foo: 33, bar: 1320});
120 - });
121 -
122 - it('createStateKeySetter should update state with mixin', () => {
123 - var instance = <TestComponentWithMixin />;
124 - instance = ReactTestUtils.renderIntoDocument(instance);
125 - expect(instance.state).toEqual({foo: 'foo'});
126 -
127 - var setter = instance.createStateKeySetter('foo');
128 -
129 - expect(instance.state).toEqual({foo: 'foo'});
130 -
131 - setter('bar');
132 - expect(instance.state).toEqual({foo: 'bar'});
133 -
134 - setter('baz');
135 - expect(instance.state).toEqual({foo: 'baz'});
136 - });
137 -
138 - it('createStateKeySetter is memoized with mixin', () => {
139 - var instance = <TestComponentWithMixin />;
140 - instance = ReactTestUtils.renderIntoDocument(instance);
141 - expect(instance.state).toEqual({foo: 'foo'});
142 -
143 - var foo1 = instance.createStateKeySetter('foo');
144 - var bar1 = instance.createStateKeySetter('bar');
145 -
146 - var foo2 = instance.createStateKeySetter('foo');
147 - var bar2 = instance.createStateKeySetter('bar');
148 -
149 - expect(foo2).toBe(foo1);
150 - expect(bar2).toBe(bar1);
151 - });
152 -});