HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-02-21
1 <p>The useState() hook works with the state inside components.</p>
1 <p>The useState() hook works with the state inside components.</p>
2 <p>Unlike class components, hooks do everything at once: initialization, updating, and providing access to the state. Let us observe a call, for example:</p>
2 <p>Unlike class components, hooks do everything at once: initialization, updating, and providing access to the state. Let us observe a call, for example:</p>
3 <p>See the Pen<a>js_react_hooks_intro-1</a>by Hexlet (<a>@hexlet</a>) on<a>CodePen</a>.</p>
3 <p>See the Pen<a>js_react_hooks_intro-1</a>by Hexlet (<a>@hexlet</a>) on<a>CodePen</a>.</p>
4 <p>The useState() hook takes the initial state as input and returns an array of two values:</p>
4 <p>The useState() hook takes the initial state as input and returns an array of two values:</p>
5 <ul><li>The current state value</li>
5 <ul><li>The current state value</li>
6 <li>A function that updates the state</li>
6 <li>A function that updates the state</li>
7 </ul><p>This code looks unusual, so there are still questions about its operation. As we know, the component, as a function, is called for each re-rendering. And it's logical to assume that the count value will always be 0 because we are constantly calling useState().</p>
7 </ul><p>This code looks unusual, so there are still questions about its operation. As we know, the component, as a function, is called for each re-rendering. And it's logical to assume that the count value will always be 0 because we are constantly calling useState().</p>
8 <p>Strange as it may seem, this won't happen. Hooks are much funkier than they seem at first glance. Yes, we call the useState() function every re-rendering occurs, but it knows about it internally and considers it in its work. We set the initial state and do not use it further. The state itself is stored somewhere inside, hidden from direct change. The only way to influence it is to call the returned function and pass a new state.</p>
8 <p>Strange as it may seem, this won't happen. Hooks are much funkier than they seem at first glance. Yes, we call the useState() function every re-rendering occurs, but it knows about it internally and considers it in its work. We set the initial state and do not use it further. The state itself is stored somewhere inside, hidden from direct change. The only way to influence it is to call the returned function and pass a new state.</p>
9 <p>The setCount() function can be called anywhere:</p>
9 <p>The setCount() function can be called anywhere:</p>
10 <ul><li>In a callback, as in the example above</li>
10 <ul><li>In a callback, as in the example above</li>
11 <li>In another component to which we can pass this function</li>
11 <li>In another component to which we can pass this function</li>
12 </ul><p>In this sense, everything works the same as in classes.</p>
12 </ul><p>In this sense, everything works the same as in classes.</p>
13 <p>Unlike this.setState(), the useState() hook does not merge the old state with the new one:</p>
13 <p>Unlike this.setState(), the useState() hook does not merge the old state with the new one:</p>
14 <p>We can use hooks more than once if needed. We can easily create multiple state variables:</p>
14 <p>We can use hooks more than once if needed. We can easily create multiple state variables:</p>
15 <h2>How many state variables should you create?</h2>
15 <h2>How many state variables should you create?</h2>
16 <p>Technically, we can put everything in one variable, as we did with classes, but with hooks, there isn't much need for this. Moreover, the state within a single component is easier to manage when it's divided into parts that are updated together. For example:</p>
16 <p>Technically, we can put everything in one variable, as we did with classes, but with hooks, there isn't much need for this. Moreover, the state within a single component is easier to manage when it's divided into parts that are updated together. For example:</p>
17  
17