{
  "id": 10997949,
  "title": "useState in React",
  "url": "https://urgent.news/2026/09/30/usestate-in-react",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-09-30T17:47:35.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/abishek_m_82/usestate-in-react-47c5"
  },
  "original_language": "en",
  "account": "In the realm of interactive web application development using React, the concept of state plays a pivotal role in managing values that undergo changes throughout the application's lifecycle. One such mechanism that facilitates this is the useState Hook. This Hook empowers functional components to create and maintain state, enabling them to retain values between renders and subsequently update the user interface accordingly.\n\nThe syntax for utilizing useState is relatively straightforward: const [ state, setState ] = useState(initialValue); In this statement, state represents the current value that the component retains, setState is a function responsible for updating the state, and initialValue serves as the initial value assigned to the state variable.\n\nFor instance, consider the following code snippet: const [ count, setCount ] = useState(0); Here, count symbolizes the current count value, while setCount is the method employed to modify this count value. The initial value, in this case, is set to 0.\n\nTo import useState into your React project, you must first import it from the React library: import { useState } from 'react'; Once imported, useState can be seamlessly integrated into functional components.\n\nUnderstanding the interplay between state and its corresponding setter function is crucial. Let's elucidate this with an example: const [ count, setCount ] = useState(0); Initially, the value of count is set to 0. To modify this value to 5, the following operation is performed: setCount(5); Following this state update, React re-renders the component, thereby making the new value of count available for usage.\n\nDirect modifications to the state variable, such as count = 5; are discouraged, as they bypass the intended functionality of the setter function. Instead, the recommended approach is to employ the setter function: setCount(5); // ✅ This notation informs React that the state necessitates an update.\n\nTo grasp the practical application of useState, let's delve into an example involving a simple counter component: import { useState } from 'react'; function App() { const [ count, setCount ] = useState(0); return ( <h1>{count}</h1> <button onClick={() => setCount(count + 1)}>Increase</button> ); } export default App; When the component is initially rendered, the screen displays the value 0. Upon clicking the \"Increase\" button, the setCount(count + 1) function is invoked, triggering the state update. React subsequently re-renders the component, resulting in the display of the updated count value.\n\nThe concept of re-rendering is closely tied to the utilization of useState. Re-rendering refers to the process where React executes the component again to ascertain the updated user interface based on the modified state. For instance, if count holds a value of 0 and the following operation is executed: setCount(1); React schedules the state update and subsequently re-renders the component with the revised state. Importantly, this re-rendering process occurs without necessitating a complete page refresh, allowing for seamless UI updates.\n\nState can be perceived as a snapshot for each render, capturing the value of a state variable within the specific render context. For example: function App() { const [ count, setCount ] = useState(0); function handleClick() { setCount(count + 1); console.log(count); } return ( <button onClick={handleClick}>{count}</button> ); } In this scenario, the count value remains 0 within the same render execution. Upon invoking setCount(1), React processes the state update, and the updated value of 1 is reflected in the subsequent render. This phenomenon is often referred to as state being a snapshot.\n\nIn certain scenarios, the new state value may depend on the previous state value. To accommodate such dependencies, functional updaters can be employed: setCount(prev => prev + 1); Here, prev represents the state value passed to the updater function. Let's consider the following sequence: setCount(prev => prev + 1); setCount(prev => prev + 1); Assuming the initial value is 0, React sequentially executes the updater functions, resulting in the final value of 2. This approach is especially beneficial when multiple updates rely on the previous state value.\n\nLastly, it is worth noting that the syntax for utilizing useState incorporates JavaScript array destructuring: const [ count, setCount ] = useState(0); In this syntax, the first element of the array denotes the current state value, whereas the second element represents the setter function. The names assigned to these variables can be customized based on the specific context of the state being managed. Examples include naming the state variable as name and its corresponding setter function as setName.",
  "summary": "When building interactive web applications with React, we often need to store values that can change over time. For example, a counter value can increase, a user's name can change, or a login status can switch between logged in and logged out. React provides a Hook called useState to handle these changing values. What is useState? useState is a React Hook that allows functional components to…",
  "key_points": [
    "useState Hook enables functional components to create and maintain state",
    "Syntax: const [ state, setState ] = useState(initialValue)",
    "Direct state modifications discouraged, use setter function setState"
  ],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}