Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81152 views
1
/**
2
* Copyright 2013-2014, 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;
105
106