Great question and something that beginners struggle with more often, so no worries if it doesn’t make sense straightaway. In React, a component is nothing more but a regular function that returns JSX. A re-render is the function being called again, because something changes, for example when a prop has changed its value. Every expression in the component is re-run on each render. So a statement like ‘let count = 0’ will re-run when the component re-renders, meaning that if something triggers a re-render, the count variable will always be re-initiated to 0. This is not desirable in a lot of cases, which is why React exposes the useState API. useState ensures that whatever the value of a state variable is, that that value will be the same when the component re-renders. In addition to that, if you use the setter function of useState, not only do you change the value, you also trigger a re-render, because there might be other parts of the component relying on the state variable. Does this help? Answer from mauricekleine on reddit.com
🌐
W3Schools
w3schools.com › react › react_usestate.asp
React useState Hook
React Compiler React Quiz React Exercises React Syllabus React Study Plan React Server React Interview Prep React Bootcamp React Certificate ... The React useState Hook allows us to track state in a function component.
🌐
React
react.dev › reference › react › useState
useState – React
It should be pure, should take no arguments, and should return a value of any type. React will call your initializer function when initializing the component, and store its return value as the initial state. See an example below. useState returns an array with exactly two values:
Discussions

What problem does useState in React solve?
Great question and something that beginners struggle with more often, so no worries if it doesn’t make sense straightaway. In React, a component is nothing more but a regular function that returns JSX. A re-render is the function being called again, because something changes, for example when a prop has changed its value. Every expression in the component is re-run on each render. So a statement like ‘let count = 0’ will re-run when the component re-renders, meaning that if something triggers a re-render, the count variable will always be re-initiated to 0. This is not desirable in a lot of cases, which is why React exposes the useState API. useState ensures that whatever the value of a state variable is, that that value will be the same when the component re-renders. In addition to that, if you use the setter function of useState, not only do you change the value, you also trigger a re-render, because there might be other parts of the component relying on the state variable. Does this help? More on reddit.com
🌐 r/webdev
57
154
November 19, 2022
What is useState() in React?
You can also look in the source code to understand how useState is implemented. Here is the definition as of version 16.9. ... React hooks are a new way (still being developed) to access the core features of react such as state without having to use classes, in your example if you want to increment ... More on stackoverflow.com
🌐 stackoverflow.com
How to use Set with react's useState?
Explore Stack Internal ... I have an array that I have to add/remove elements from, and I figured I would use Set to accomplish this because of its add has and delete. ... You can't since a Set is not an immutable data structure and React's state needs to be immutable. More on stackoverflow.com
🌐 stackoverflow.com
Can someone please explain the difference between useState and useReducer hook like I'm 5?
Their goal is the same: keeping state. With useState, you keep track of a single variable at a time: UseReducer allows for more complex state keeping, but you’ll have to write a function to merge new state with previously existing state yourself. The advantage is often you can dispatch “actions” on your state, the standard example being “increment” and “decrement”. So instead of updating state to state + 1, you can simply dispatch “increment”. Basically: as you write the implementation yourself, you have more freedom in how state is structured and how it’s updated based on existing state. More on reddit.com
🌐 r/reactjs
68
166
January 7, 2022
🌐
GeeksforGeeks
geeksforgeeks.org › reactjs › reactjs-usestate-hook
React useState Hook - GeeksforGeeks
The useState() hook allows you to add state to functional components in React.
Published   January 10, 2026
🌐
React
legacy.reactjs.org › docs › hooks-state.html
Using the State Hook – React
Normally, variables “disappear” when the function exits but state variables are preserved by React. What do we pass to useState as an argument? The only argument to the useState() Hook is the initial state. Unlike with classes, the state doesn’t have to be an object.
🌐
Reddit
reddit.com › r/webdev › what problem does usestate in react solve?
r/webdev on Reddit: What problem does useState in React solve?
November 19, 2022 -

Im not good in javascript and we started doing React in school.

Ive seen countless videoes on useState and read about it, but i dont get what problem its trying to solve. They all just say how to use it, not why to use it. It just seems like a variable with extra steps.

Like if i wanted to make a counter, i could:

const [count, setCount] = useState(0)

Now i have a variable, with an initial value, and which i can only update with a function (for some reason, why is that smart?).

Why wouldnt i just write:

Let count = 0

Shouldnt that suffice? I can add and subtract to that too, and i dont need a specific function to do so. When i refresh the page, both values resets regardless.

Top answer
1 of 21
479
Great question and something that beginners struggle with more often, so no worries if it doesn’t make sense straightaway. In React, a component is nothing more but a regular function that returns JSX. A re-render is the function being called again, because something changes, for example when a prop has changed its value. Every expression in the component is re-run on each render. So a statement like ‘let count = 0’ will re-run when the component re-renders, meaning that if something triggers a re-render, the count variable will always be re-initiated to 0. This is not desirable in a lot of cases, which is why React exposes the useState API. useState ensures that whatever the value of a state variable is, that that value will be the same when the component re-renders. In addition to that, if you use the setter function of useState, not only do you change the value, you also trigger a re-render, because there might be other parts of the component relying on the state variable. Does this help?
2 of 21
34
Here's a demonstration of the difference: https://playcode.io/1015207 There are 2 things that useState is used for in this context: Maintaining the state across re-renders Triggering a re-render You can see both of those in action in the example above. Function components are just functions, so if something triggers a re-render of your component and you've used a regular variable that's initialised in the function itself (e.g. by using let count = 0) then it will always be reset its initial value on re-render. The useState hook stores this state elsewhere, and so is able to ensure your state is retained correctly across re-renders. When the component is unmounted, the state is released. The action of updating a component's state also triggers a re-render in itself though. In function components, if you just update a variable somewhere or modify the DOM manually, etc. then React isn't aware of it, so won't update what's rendered. Again, you can see this in the example above, attempting to update the count variable doesn't trigger a re-render. You can also see in the example that if you click to increment count, and then click to increment the hook-based count that only the hook based count increases, showing the value for count must have been reset when the re-render was triggered.
🌐
Medium
medium.com › @titoadeoye › react-hooks-usestate-with-practical-examples-64abd6df6471
React Hooks: useState (With Practical Examples) | by Tito Adeoye | Medium
February 5, 2024 - The useState hook lets us create a state variable, initialize it with data and also gives us access to a setter function that lets us update this state. This is especially important in React which, just like the name suggests, is a library that ...
🌐
LogRocket
blog.logrocket.com › home › usestate in react: a complete guide
useState in React: A complete guide - LogRocket Blog
October 22, 2024 - React useState allows you to add state to functional components, returning an array with two values: current state and a function to update it.
Find elsewhere
🌐
Hygraph
hygraph.com › blog › usestate-react
useState() Hook in React - A Complete Guide | Hygraph
January 21, 2026 - In this guide, we have learned what state is and why it is important, we learned about the useState() hook from React which helps us to manage a component’s memory. We also learned about the lifecycle of a state variable and how the new state value is enforced after a component’s re-render.
Top answer
1 of 14
244

React hooks are a new way (still being developed) to access the core features of react such as state without having to use classes, in your example if you want to increment a counter directly in the handler function without specifying it directly in the onClick prop, you could do something like:

...
const [count, setCounter] = useState(0);
const [moreStuff, setMoreStuff] = useState(...);
...

const setCount = () => {
    setCounter(count + 1);
    setMoreStuff(...);
    ...
};

and onClick:

<button onClick={setCount}>
    Click me
</button>

Let's quickly explain what is going on in this line:

const [count, setCounter] = useState(0);

useState(0) returns a tuple where the first parameter count is the current state of the counter and setCounter is the method that will allow us to update the counter's state. We can use the setCounter method to update the state of count anywhere - In this case we are using it inside of the setCount function where we can do more things; the idea with hooks is that we are able to keep our code more functional and avoid class based components if not desired/needed.

I wrote a complete article about hooks with multiple examples (including counters) such as this codepen, I made use of useState, useEffect, useContext, and custom hooks. I could get into more details about how hooks work on this answer but the documentation does a very good job explaining the state hook and other hooks in detail.

update: Hooks are not longer a proposal, since version 16.8 they're now available to be used, there is a section in React's site that answers some of the FAQ.

2 of 14
78

useState is one of build-in react hooks available in 0.16.7 version.

useState should be used only inside functional components. useState is the way if we need an internal state and don't need to implement more complex logic such as lifecycle methods.

const [state, setState] = useState(initialState);

Returns a stateful value, and a function to update it.

During the initial render, the returned state (state) is the same as the value passed as the first argument (initialState).

The setState function is used to update the state. It accepts a new state value and enqueues a re-render of the component.

Please note that useState hook callback for updating the state behaves differently than components this.setState. To show you the difference I prepared two examples.

class UserInfoClass extends React.Component {
  state = { firstName: 'John', lastName: 'Doe' };
  
  render() {
    return <div>
      <p>userInfo: {JSON.stringify(this.state)}</p>
      <button onClick={() => this.setState({ 
        firstName: 'Jason'
      })}>Update name to Jason</button>
    </div>;
  }
}

// Please note that new object is created when setUserInfo callback is used
function UserInfoFunction() {
  const [userInfo, setUserInfo] = React.useState({ 
    firstName: 'John', lastName: 'Doe',
  });

  return (
    <div>
      <p>userInfo: {JSON.stringify(userInfo)}</p>
      <button onClick={() => setUserInfo({ firstName: 'Jason' })}>Update name to Jason</button>
    </div>
  );
}

ReactDOM.render(
  <div>
    <UserInfoClass />
    <UserInfoFunction />
  </div>
, document.querySelector('#app'));
<script src="https://unpkg.com/[email protected]/umd/react.development.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script>

<div id="app"></div>

New object is created when setUserInfo callback is used. Notice we lost lastName key value. To fixed that we could pass function inside useState.

setUserInfo(prevState => ({ ...prevState, firstName: 'Jason' })

See example:

// Please note that new object is created when setUserInfo callback is used
function UserInfoFunction() {
  const [userInfo, setUserInfo] = React.useState({ 
    firstName: 'John', lastName: 'Doe',
  });

  return (
    <div>
      <p>userInfo: {JSON.stringify(userInfo)}</p>
      <button onClick={() => setUserInfo(prevState => ({
        ...prevState, firstName: 'Jason' }))}>
        Update name to Jason
      </button>
    </div>
  );
}

ReactDOM.render(
    <UserInfoFunction />
, document.querySelector('#app'));
<script src="https://unpkg.com/[email protected]/umd/react.development.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script>

<div id="app"></div>

Unlike the setState method found in class components, useState does not automatically merge update objects. You can replicate this behavior by combining the function updater form with object spread syntax:

setState(prevState => {
  // Object.assign would also work
  return {...prevState, ...updatedValues};
});

For more about useState see official documentation.

🌐
Contentful
contentful.com › blog › react-usestate-hook
React useState hook: Complete guide and tutorial | Contentful
May 13, 2025 - The React useState hook adds a state variable to a function component. Setting a state variable gives your component the ability to remember things across page re-renders, so that data can be stored and the component can be updated when the ...
🌐
DEV Community
dev.to › mikhaelesa › what-is-usestate-in-react-47io
What is useState in React? - DEV Community
October 17, 2023 - useState is a React hook used in functional components to manage and update component-specific state. It takes an initial value and returns an array with the current state value and a function to update that state.
🌐
GeeksforGeeks
geeksforgeeks.org › reactjs › what-is-usestate-in-react
What is useState() in React ? - GeeksforGeeks
January 9, 2025 - The useState() is a Hook that allows you to have state variables in functional components . so basically useState is the ability to encapsulate local state in a functional component.
🌐
Dave Ceddia
daveceddia.com › usestate-hook-examples
4 Examples of the useState Hook
July 12, 2020 - The “magic” here is that React ... “state cells.” When you call useState, React stores that state in the next available cell, and increments the pointer (the array index)....
🌐
GUVI
guvi.in › blog › mern › usestate() hook in react for beginners | react hooks 2025
useState() Hook in React for Beginners | React Hooks 2025
October 27, 2025 - useState() hook in react allows you to add state to functional components. It returns an array with two elements: the current state and a function to update it
🌐
freeCodeCamp
freecodecamp.org › news › usestate-hook-3-different-examples
How to Use the useState() Hook in React – Explained with Code Examples
May 8, 2023 - Once you click the sign out button, you are prompted to sign in, with the 'please sign in' message. This example will help show how you can use useState() to update your state through clicks. The idea behind this simple counter is that your clicks are counted. So, if you click the button 12 times the counter updates to 12. Note that the button updates on every click/count. import React from 'react'; const Newcounter = () => { return ( <div> <button type="button">You will see the count here</button> </div> ) } export default Newcounter;
🌐
Horilla
horilla.com › blogs › what-is-usestate-hook-in-react
What is useState Hook in React & How to Manage State in Functional Components? | Blogs | Free HRMS | Horilla
February 3, 2024 - The useState hook is a function provided by React that enables functional components to hold and update state, just like class components with ‘this.state’. With useState, you can add stateful behavior to functional components without converting ...
🌐
Robin Wieruch
robinwieruch.de › react-usestate-hook
How to useState in React
May 30, 2019 - Therefore, a good rule of thumb may be: always use a function in useState's update function if your state update depends on your previous state. React's useState is the go-to hook to manage state.
🌐
React
react.dev › learn › state-a-components-memory
State: A Component's Memory – React
Every time your component renders, useState gives you an array containing two values: The state variable (index) with the value you stored.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-use-the-usestate-and-useeffect-hooks-in-your-project
React Hooks – How to Use the useState & useEffect Hooks in Your Project
October 8, 2024 - The way useState works is that it gives us two variables. The first variable is known as the value of the state, and the second variable is a function used to update the state. ... import './App.css' import React, { useState } from 'react'; ...