DEV Community

Cover image for What is useState?with example?
Arul .A
Arul .A

Posted on

What is useState?with example?

  • useState is a React Hook used to stores the value and update state in functional components. It returns a state variable and a setter function. Whenever the state changes, React triggers a re-render to update the UI.

Syntax:

const[Variable,setVariable]=useState(initial value)
Enter fullscreen mode Exit fullscreen mode

Example:

import { useState } from "react";

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

  function increment() {
    setCount(count + 1); 
  }

  return (
    <div>
      <p>Current Count: {count}</p>
      <button onClick={increment}>Increase</button>
    </div>
  );
}

export default Counter;

Enter fullscreen mode Exit fullscreen mode

Top comments (0)