DEV Community

Cover image for ⚛️ React Hooks - useState
Marcos Mendes
Marcos Mendes

Posted on • Edited on

⚛️ React Hooks - useState

In this post, we will know and understand more about the most used Hook and the first to be created by the React team, the useState.

This is probably the Hook you'll use the most. As the name implies, it gives us the "power" to use states in functional components.

useState receives a parameter that is the initial value of the state and returns an array with two positions, the first of which is the state value and the second is a function that will be responsible for changing (change) the value of this state.

A feature of useState is that we can, in the function that updates the state, send a function as an argument and in this function capture the current state value in the same way as in setState in classes.

This becomes very useful when we need to update a state based on its previous value, as for example in a toggle function where each click changes the state between true and false:

Top comments (0)