DEV Community

Sathish K
Sathish K

Posted on

UseState in React.js

What is UseState ?
The UseState is the hooks in React.js. It is the fundamental hook is allow to functional components to manage and update the internal state.It is a inbuilt function.Its add to state variables to functional components.

Syntax:

const[state,setState]=usestate(0)
Enter fullscreen mode Exit fullscreen mode
Example:

import React, { useState } from 'react';

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

  const increment = () => {
    setCount(prevCount => prevCount + 1); // Update count based on previous state
  };

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

export default Counter;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)