Added docs about the first-parameter-is-function version of setState.
Jim committed
Mar 10, 2015 at 10:49 UTC
7569e810e77eb7daa9a93128e454e50e77511e64
1 file changed
+19
-2
docs/docs/ref-02-component-api.md
+19
-2
@@ -14,10 +14,27 @@ Instances of a React Component are created internally in React when rendering. T
14
### setState
15
16
```javascript
17
-setState(object nextState[, function callback])
17
+setState(function|object nextState[, function callback])
18
```
19
+Merges nextState with the current state. This is the primary method you use to trigger UI updates from event handlers and server request callbacks.
20
20
-Merges nextState with the current state. This is the primary method you use to trigger UI updates from event handlers and server request callbacks. In addition, you can supply an optional callback function that is executed once `setState` is completed and the component is re-rendered.
21
+The first argument can be an object (containing zero or more keys to update) or a function (of state and props) that returns an object containing keys to update.
22
+
23
+Here is the simple object usage...
24
+
25
+```javascript
26
+setState({mykey: 'my new value'});
27
+```
28
+
29
+It's also possible to pass a function with the signature `function(state, props)`. This can be useful in some cases when you want to enqueue an atomic update that consults the previous value of state+props before setting any values. For instance, suppose we wanted to increment a value in state:
30
+
31
+```javascript
32
+setState(function(previousState, currentProps) {
33
+ return {myInteger: previousState.myInteger + 1};
34
+});`
35
+```
36
+
37
+The second (optional) parameter is a callback function that will be executed once `setState` is completed and the component is re-rendered.
38
39
> Notes:
40
>