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