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:
- The current state value (
count), initialized to0. - A function to update that value (
setCount).
We use array destructuring to grab them both!
Try It Yourself