You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/docs/faq-state.md
+4-2Lines changed: 4 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,9 @@ category: FAQ
12
12
13
13
### Why is `setState` is giving me the wrong value?
14
14
15
-
Calls to `setState` are batched, so it is possible to "lose" an update if you call it with the partial object syntax:
15
+
Calls to `setState` are asynchronous - don't rely on `this.state` to reflect the new value immediately after calling `setState`. Pass an updater function instead of an object if you need compute values based on the current state (see below for details).
16
+
17
+
Example of code that will not behave as expected:
16
18
17
19
```jsx
18
20
incrementCount= () => {
@@ -28,7 +30,7 @@ handleSomething() {
28
30
29
31
See below for how to fix this problem.
30
32
31
-
### How do I do ordered state updates?
33
+
### How do I update state with values that depend on the current state?
32
34
33
35
Pass a function instead of an object to setState to ensure the call always uses the most updated version of state (see below).
0 commit comments