Sobes.tech
Middle+

What is the state of a component?

sobes.tech AI

Answer from AI

Component state is a set of data that determines the current behavior and appearance of a component in the user interface. In React, for example, state is stored within the component and can change over time, triggering a re-render to update the UI.

Example: a counter that increases when clicked stores its current value in the state.

import React, { useState } from 'react';

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

  return (
    <div>
      <p>Counter: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increase</button>
    </div>
  );
}

Thus, state allows a component to be interactive and respond dynamically to user actions or other events.