By the end of this lesson, you will:
useState, useEffect, and useRef to manage state and side effects.In React, components go through a series of phases:
In class components, we used methods like:
componentDidMount()componentDidUpdate()componentWillUnmount()But in modern React, we use Hooks in function components to handle the same things.
✅ Allow you to use state and lifecycle features in function components
✅ Encourage cleaner, more modular code
✅ Avoid “this” binding headaches
✅ Used in every modern React project
useState — for Local Stateimport { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times.</p>
<button onClick={() => setCount(count + 1)}>Click Me</button>
</div>
);
}
✅ Keeps track of changing values
✅ useState(initialValue) returns a pair: [value, setValue]
useEffect — for Side Effectsimport { useEffect } from 'react';
useEffect(() => {
console.log('Component mounted');
return () => {
console.log('Component unmounted');
};
}, []);
✅ Replaces lifecycle methods:
componentDidMount → useEffect(..., [])componentDidUpdate → useEffect(..., [dependency])componentWillUnmount → return a cleanup functionuseRef — for Persistent Referencesimport { useRef } from 'react';
function Timer() {
const timerId = useRef(null);
const startTimer = () => {
timerId.current = setInterval(() => {
console.log('Tick...');
}, 1000);
};
const stopTimer = () => {
clearInterval(timerId.current);
};
return (
<div>
<button onClick={startTimer}>Start</button>
<button onClick={stopTimer}>Stop</button>
</div>
);
}
✅ useRef() returns a mutable object
✅ Doesn’t cause re-renders
✅ Great for DOM access, timers, or previous values
| Phase | Hook Used | Purpose |
|---|---|---|
| Mount | useEffect(() => {}, []) |
Run once when component loads |
| Update | useEffect(() => {}, [value]) |
Run when specific values change |
| Unmount | return () => {} inside useEffect |
Cleanup logic like removing listeners |
import { useState, useEffect } from 'react';
function Clock() {
const [time, setTime] = useState(new Date());
useEffect(() => {
const timer = setInterval(() => {
setTime(new Date());
}, 1000);
// Cleanup
return () => clearInterval(timer);
}, []);
return <h2>{time.toLocaleTimeString()}</h2>;
}
✅ Updates every second
✅ Uses useEffect for mounting and cleanup
Use useState to count how many times a button is clicked.
useEffect for a TimerCreate a component that shows how long the user has been on the page.
Create a component that logs a message when it’s removed from the DOM (use useEffect cleanup).
✅ Use multiple useEffect hooks for separation of concerns
✅ Avoid heavy logic inside useEffect
✅ Always clean up side effects
✅ Don’t overuse useRef — only when needed
useEffect?useState different from a regular variable?useRef?Build a small React component that:
useState to manage a valueuseEffect to log something on mountuseRef to track how many times it’s been rendered
Not a member yet? Register now
Are you a member? Login now