DEV Community

GageHarmon
GageHarmon

Posted on • Updated on

React: useState Hooks

React is one of the more popular libraries used for building the front end in your JavaScript. It provides Developers with quite a few powerful tools, one of those being the useState hook.

So what is useState?

The useState hook is a function provided by React that allows us to add "state" to a functional component. Before the introduction of hooks, the state could only be managed in class components using the setState method.

The useState hook takes an initial state value and returns two values: the current state value and a function that can be used to update the state value.

Below is an example of how to use useState in your React component:
///////////////////////////////////////////////////////////
import React, { useState } from 'react';

function Example() {
const [count, setCount] = useState(0);

return (


You clicked {count} times


setCount(count + 1)}>
Click me


);
}

In the above code snippet, we define a component named "Example" that uses the useState hook to add a state to our component. We define a state variable called count and a function called setCount that will be used to update the value of the count.

When the component is initially rendered, the starting value of count is set to 0. We then render a "p" element that displays the current value of count, followed by a button element that, when clicked, calls the setCount function and updates the value of count.

So how does useState work then?

Behind the scenes, React keeps track of the state value and re-renders the component whenever the state value changes. When the setCount function is called, React will update the state value and trigger a re-render of our component.

It's important to note that calling the setCount function does not immediately update the state value. Instead, React schedules an update and batches multiple state updates together for performance reasons. This means that if we call setCount multiple times in a row, React will only perform a single re-render after all the state updates have been applied.

Another important thing to keep in mind is that state updates in React are asynchronous. This means that if we need to perform some action after a state update has occurred, we should use the useEffect hook to run our code after the component has re-rendered.

Below is an example of using the useEffect hook to run some code after a state update:
///////////////////////////////////////////////////////////
import React, { useState, useEffect } from 'react';

function Example() {
const [count, setCount] = useState(0);

useEffect(() => {
document.title = You clicked ${count} times;
}, [count]);

return (


You clicked {count} times


setCount(count + 1)}>
Click me


);
}

In the above example, we implement the useEffect hook to update the title of our page whenever the count state value changes. The useEffect hook takes a function as its first argument and an array of dependencies as its second argument. Whenever any of the dependencies change, the function will be called.

In conclusion a useState hook is a powerful tool provided by React that allows us to add a state to functional components. We can build more dynamic and interactive user interfaces by understanding how it works and how to use them.

Top comments (0)