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

useEffect Hook

Lesson 2 of 17 Intermediate Interactive

useEffect runs side effects in functional components — data fetching, subscriptions, or DOM manipulation.

Syntax

REACT
import { useEffect, useState } from 'react';

function DataFetcher() {
    const [data, setData] = useState(null);

    useEffect(() => {
        fetch('/api/data')
            .then(res => res.json())
            .then(setData);
    }, []);
}
useEffect Hook
REACT
import { useState, useEffect } from "react";

function Timer() {
    const [seconds, setSeconds] = useState(0);

    useEffect(() => {
        const interval = setInterval(() => {
            setSeconds(s => s + 1);
        }, 1000);

        return () => clearInterval(interval);
    }, []);

    return <p>Timer: {seconds}s</p>;
}

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.