UNPKG

3.52 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2013-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 */
8
9'use strict';
10
11var ReactStateSetters = {
12 /**
13 * Returns a function that calls the provided function, and uses the result
14 * of that to set the component's state.
15 *
16 * @param {ReactCompositeComponent} component
17 * @param {function} funcReturningState Returned callback uses this to
18 * determine how to update state.
19 * @return {function} callback that when invoked uses funcReturningState to
20 * determined the object literal to setState.
21 */
22 createStateSetter: function (component, funcReturningState) {
23 return function (a, b, c, d, e, f) {
24 var partialState = funcReturningState.call(component, a, b, c, d, e, f);
25 if (partialState) {
26 component.setState(partialState);
27 }
28 };
29 },
30
31 /**
32 * Returns a single-argument callback that can be used to update a single
33 * key in the component's state.
34 *
35 * Note: this is memoized function, which makes it inexpensive to call.
36 *
37 * @param {ReactCompositeComponent} component
38 * @param {string} key The key in the state that you should update.
39 * @return {function} callback of 1 argument which calls setState() with
40 * the provided keyName and callback argument.
41 */
42 createStateKeySetter: function (component, key) {
43 // Memoize the setters.
44 var cache = component.__keySetters || (component.__keySetters = {});
45 return cache[key] || (cache[key] = createStateKeySetter(component, key));
46 }
47};
48
49function createStateKeySetter(component, key) {
50 // Partial state is allocated outside of the function closure so it can be
51 // reused with every call, avoiding memory allocation when this function
52 // is called.
53 var partialState = {};
54 return function stateKeySetter(value) {
55 partialState[key] = value;
56 component.setState(partialState);
57 };
58}
59
60ReactStateSetters.Mixin = {
61 /**
62 * Returns a function that calls the provided function, and uses the result
63 * of that to set the component's state.
64 *
65 * For example, these statements are equivalent:
66 *
67 * this.setState({x: 1});
68 * this.createStateSetter(function(xValue) {
69 * return {x: xValue};
70 * })(1);
71 *
72 * @param {function} funcReturningState Returned callback uses this to
73 * determine how to update state.
74 * @return {function} callback that when invoked uses funcReturningState to
75 * determined the object literal to setState.
76 */
77 createStateSetter: function (funcReturningState) {
78 return ReactStateSetters.createStateSetter(this, funcReturningState);
79 },
80
81 /**
82 * Returns a single-argument callback that can be used to update a single
83 * key in the component's state.
84 *
85 * For example, these statements are equivalent:
86 *
87 * this.setState({x: 1});
88 * this.createStateKeySetter('x')(1);
89 *
90 * Note: this is memoized function, which makes it inexpensive to call.
91 *
92 * @param {string} key The key in the state that you should update.
93 * @return {function} callback of 1 argument which calls setState() with
94 * the provided keyName and callback argument.
95 */
96 createStateKeySetter: function (key) {
97 return ReactStateSetters.createStateKeySetter(this, key);
98 }
99};
100
101module.exports = ReactStateSetters;
\No newline at end of file