Lesson 15 of 3050%

(Progress persistence is disabled until Phase 9)

React
Beginner

useState

Using the useState hook to add state to functional components.

useState is a Hook that lets you add React state to a function component.

import React, { useState } from 'react';

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

  return <p>Count: {count}</p>;
}

useState(0) returns an array with exactly two items:

  1. The current state value (count), initialized to 0.
  2. A function to update that value (setCount).

We use array destructuring to grab them both!

Try It Yourself

Lesson Progress

Practice This TopicTake QuizBuild a React Project

Ready to practise?

Apply what you learned with a hands-on challenge.

Practise This Topic