@@ -42,6 +42,10 @@ Here, `useState` is a *Hook* (we'll talk about what this means in a moment). We
The only argument to `useState` is the initial state. In the example above, it is `0` because our counter starts from zero. Note that unlike `this.state`, the state here doesn't have to be an object -- although it can be if you want. The initial state argument is only used during the first render.
+>
+> If you are wondering why we are passing a function to `setCount`, read more about [functional updates](//docs/hooks-reference.html#functional-updates) or [general good practices with updating component state](https://reactjs.org/docs/hooks-reference.html#functional-updates).
+>In short, we can pass value or [special function](https://reactjs.org/docs/hooks-reference.html#functional-updates) that takes a previous state value and returns a new value we want to store in `count`.
+
#### Declaring multiple state variables {#declaring-multiple-state-variables}
You can use the State Hook more than once in a single component:
@@ -91,7 +95,7 @@ function Example() {
return (
@@ -174,7 +174,7 @@ In a function, we can use `count` directly:
In a class, we need to call `this.setState()` to update the `count` state:
```js{1}
- this.setState({ count: this.state.count + 1 })}>
+ this.setState(prevState => { count: prevState.count + 1 })}>
Click me
```
@@ -182,7 +182,7 @@ In a class, we need to call `this.setState()` to update the `count` state:
In a function, we already have `setCount` and `count` as variables so we don't need `this`:
```js{1}
- setCount(count + 1)}>
+ setCount(prevCount => prevCount + 1)}>
Click me
```
@@ -204,7 +204,7 @@ Let's now **recap what we learned line by line** and check our understanding.
6: return (
7: