Sobes.tech
Junior — Middle

What is the purpose of the 'state' property in React components?

sobes.tech AI

Answer from AI

The state property in React components is used to store and manage the component's internal state. It allows the component to respond to data changes and update the display without needing to reload the page.

For example, if you have a button that changes its text when clicked, you can store the current text in state and update it upon the event:

import React, { useState } from 'react';

function Example() {
  const [text, setText] = useState('Click me');

  return (
    <button onClick={() => setText('Thank you!')}>{text}</button>
  );
}

Thus, state provides dynamic behavior for components and their response to user actions or other events.