This document explains the four phases of the React function component lifecycle—mounting, updating, unmounting, and error handling—using hooks and practical examples.
This document covers the four phases of the React function component lifecycle—mounting, updating, unmounting, and error handling—explaining how hooks like useState and useEffect enable similar control as class lifecycle methods.
React function components are the core building blocks for user interfaces. Understanding their lifecycle is essential for managing state, side effects, and cleanup throughout a component’s existence.
React function components experience four main phases:
During mounting, React initializes the component and prepares it for rendering. Key steps include:
1import React, { useState } from 'react'
2
3function Counter() {
4 const [count, setCount] = useState(0)
5 return <div>Count: {count}</div>
6}
1import React, { useState, useEffect } from 'react'
2
3function DataFetcher() {
4 const [data, setData] = useState(null)
5 useEffect(() => {
6 fetch('https://api.example.com/data')
7 .then(res => res.json())
8 .then(setData)
9 }, [])
10 return <div>Data: {JSON.stringify(data)}</div>
11}
In the updating phase, React responds to changes in state or props by re-invoking the function body and re-evaluating the JSX.
1import React, { useState } from 'react'
2
3function MyComponent() {
4 const [count, setCount] = useState(0)
5 function increment() {
6 setCount(prev => prev + 1)
7 }
8 return (
9 <div>
10 <p>Count: {count}</p>
11 <button onClick={increment}>Increment</button>
12 </div>
13 )
14}
When a component is removed from the DOM, React performs cleanup operations. This is handled by returning a cleanup function from useEffect.
1import React, { useEffect } from 'react'
2
3function Timer() {
4 useEffect(() => {
5 const interval = setInterval(() => {
6 console.log('interval tick')
7 }, 1000)
8 return () => clearInterval(interval)
9 }, [])
10 return <div>Timer running...</div>
11}
If an error occurs during rendering or in a child component, React routes it to the nearest error boundary. Error boundaries are special components that catch errors and display fallback UIs, preventing the entire app from crashing.
React function component lifecycle includes mounting, updating, unmounting, and error handling. Hooks like useState and useEffect provide control over state, side effects, and cleanup, while error boundaries ensure robust error handling.
(1) The lifecycle includes mounting, updating, unmounting, and error handling.
(4) Error boundaries are not set up inside useEffect; they are special components.
| Phase | Description |
|---|---|
| A. Mounting | 1. Handles cleanup when component is removed |
| B. Updating | 2. Initializes component and state, runs effects |
| C. Unmounting | 3. Responds to state or prop changes |
| D. Error Handling | 4. Catches errors and displays fallback UI |
A-2, B-3, C-1, D-4.
useEffect with an empty dependency array runs only once after the initial render, similar to componentDidMount in class components.
True. This pattern is used for side effects that should run only on mount.
(3) Styling is not a lifecycle phase.