Skip to content
Home Tutorials Roadmaps Courses Classroom Training Blog
Log in Join free
React Intermediate Free

useState Hook

Lesson 3 of 17 · State & Lifecycle 18%
1. Getting Started
What is React?
2. Components & Props
Components & Props
6. Performance & Testing
React.memo & Lazy Loading
7. Custom Hooks
Building Custom Hooks
8. State Management
Context API Redux Basics
9. Error Boundaries
Error Handling
10. Server Components
React Server Components
11. Next.js Introduction
Next.js Basics
12. Animations & Transitions
Framer Motion

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.

Choose an answer to continue.

Interview Questions

It is a fundamental React feature.

Up next

useEffect Hook

Lesson 4 of 17 · State & Lifecycle