Preparing your workspace
useEffect lets a component synchronize with systems outside React after rendering.
An effect is useful when React needs to interact with something outside the component's rendering logic. Common examples include browser APIs, subscriptions, timers, network requests, and manually controlled third-party systems. The dependency array controls when the effect runs based on reactive values used by the effect. Effects can return a cleanup function for removing subscriptions, clearing timers, or disconnecting from external systems. Many calculations do not need an effect and can be performed directly during rendering.
A course page can use an effect to start a timer, subscribe to progress updates, or synchronize document metadata.
import { useEffect, useState } from 'react';
export default function Clock() {
const [time, setTime] = useState(new Date());
useEffect(() => {
const timer = setInterval(() => {
setTime(new Date());
}, 1000);
return () => clearInterval(timer);
}, []);
return <p>{time.toLocaleTimeString()}</p>;
}The displayed time updates every second and the timer is cleaned up when the component is removed.