Frontend
React
What is state in React and how is it used?

What is state in React and how is it used?

State in React is an object that represents the dynamic state of a component. It is used with the useState hook in functional components or in the constructor of class components.

For example:

import React, { useState } from 'react';
 
function Counter() {
  const [count, setCount] = useState(0);
 
  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}