React / Next.js

Component Lifecycle

Mounting, updating, unmounting, useEffect cleanup, dependencies array, lifecycle patterns

18 interview questionsยท
Junior
1

What is mounting in a React component?

Answer

Mounting is the phase where a component is created and inserted into the DOM for the first time. During this phase, React initializes state, executes construction code and performs the first render. This is the ideal moment to trigger API calls or initialize subscriptions via useEffect.

2

What is unmounting in a React component?

Answer

Unmounting is the phase where a component is removed from the DOM and destroyed. This phase occurs when the component is no longer needed, for example during a route change or rendering condition. This is the critical moment to clean up resources: cancel timers, close WebSocket connections and remove event listeners to prevent memory leaks.

3

When does the function passed to useEffect execute by default?

Answer

By default, useEffect executes after every component render, including the first render and all subsequent re-renders. This asynchronous execution allows not blocking the user interface. To control this behavior, it's possible to use the dependencies array as second argument to limit executions only when certain values change.

4

What is the purpose of the dependency array in useEffect?

5

What does it mean to pass an empty array [] as dependencies to useEffect?

+15 interview questions

Master React / Next.js for your next interview

Access all questions, flashcards, technical tests, code review exercises and interview simulators.

Start for free