This document explains the use of useEffect and custom hooks in React including how to manage side effects, dependency arrays, and create reusable logic for function components.
This document covers the use of useEffect and custom hooks in React. It explains how to manage side effects, use dependency arrays, and create reusable logic for function components using custom hooks.
In React, a side effect is any operation that affects the application outside the component’s render process, such as data fetching, event subscriptions, DOM manipulation, or timers. The useEffect hook allows function components to perform side effects efficiently.
useEffect runs after the component renders. 1import React, { useState, useEffect } from 'react'
2
3function FoodList() {
4 const [foods, setFoods] = useState([])
5
6 useEffect(() => {
7 fetch('https://api.example.com/foods')
8 .then(response => response.json())
9 .then(data => setFoods(data))
10 .catch(error => console.error('Error:', error))
11 }, [])
12
13 return (
14 <ul>
15 {foods.map(food => (
16 <li key={food.id}>{food.name}</li>
17 ))}
18 </ul>
19 )
20}
The dependency array determines when the effect runs:
[]): Effect runs only once on mount.Custom hooks allow you to extract and reuse logic across multiple components. They are named with the use prefix and can combine standard hooks like useState and useEffect.
1import { useState } from 'react'
2
3function useToggle(initialValue = false) {
4 const [isToggled, setIsToggled] = useState(initialValue)
5 const toggle = () => setIsToggled(prev => !prev)
6 return [isToggled, toggle]
7}
8
9export default useToggle
1import React from 'react'
2import useToggle from './useToggle'
3
4function ToggleButton() {
5 const [isToggled, toggle] = useToggle()
6 return <button onClick={toggle}>{isToggled ? 'On' : 'Off'}</button>
7}
The useEffect hook enables function components to manage side effects, while custom hooks allow for reusable, modular logic. By understanding dependency arrays and custom hook patterns, developers can write cleaner and more maintainable React code.
(1) useEffect is used to perform side effects such as data fetching, subscriptions, or DOM updates in function components.
(2) Omitting the dependency array causes the effect to run after every render.
(4) Custom hooks cannot be used in class components; they are for function components only.
(1) Dependency arrays determine when the effect should re-run based on changes to listed values.
(1) Ensure the custom hook is using the correct state and effect hooks and is implemented properly.
| Scenario | Behavior |
|---|---|
| A. [] | 1. Runs after every render |
| B. [count] | 2. Runs only once on mount |
| C. (no array) | 3. Runs when count changes |
A-2, B-3, C-1.
Custom hooks allow you to extract and reuse logic across multiple React components.
True. Custom hooks enable logic reuse and cleaner code across components.
(3) useEffect is only used in function components, not class components.
(1) Custom hooks enable logic reuse and cleaner code across components.
(1) useToggle manages a boolean state and provides a function to toggle its value.