DevPath · Learn to code ESPTEN

Components, JSX and state

State with useState

State

Props come from the outside and do not change. State is information that the component manages and can change over time (a counter, text being typed...). When state changes, React re-renders the component.

State is declared with the useState hook:

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

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>Add</button>
    </div>
  );
}

In the exercises, useState (and other hooks) are already available: you can use them directly, without importing them.

Put this into practice

DevPath is a hands-on course: you read the theory here; in the app you put it into practice with exercises that really run, offline.

Start free in the app →
← Props: passing data to a componentView the module →