Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials React useState Hook
React Intermediate FREE

useState Hook

Lesson 1 of 17 Intermediate Interactive

useState is a React Hook that adds state to functional components.

Syntax

REACT
import { useState } from 'react';

function Counter() {
    const [count, setCount] = useState(0);
    return (
        <button onClick={() => setCount(count + 1)}>
            Count: {count}
        </button>
    );
}
useState Hook
REACT
import { useState } from "react";

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

    return (
        <div>
            <p>Count: {count}</p>
            <button onClick={() => setCount(count + 1)}>
                Increment
            </button>
            <button onClick={() => setCount(count - 1)}>
                Decrement
            </button>
        </div>
    );
}

Practice

1
Exercise

Practice this concept.

Answer
Write the code as shown above.

Quick Quiz

1

What did you learn?

This covers the basics.

Interview Questions

It is a fundamental React feature.