DEV Community

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

Posted on • Edited on

9

⚛️ 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:

AWS Q Developer image

Your AI Code Assistant

Implement features, document your code, or refactor your projects.
Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay