Loading

Quipoin Menu

Learn • Practice • Grow

react / Component Lifecycle in React
interview

Q1. What is the component lifecycle in React?
The component lifecycle refers to the different phases a React component goes through: mounting (when it is added to the DOM), updating (when props or state change), and unmounting (when it is removed from the DOM). Class components have lifecycle methods for each phase.

Q2. What are the main lifecycle methods in class components?
Main methods: constructor (initialization), render (returns JSX), componentDidMount (after first render), componentDidUpdate (after updates), componentWillUnmount (before removal). Others like shouldComponentUpdate can optimize performance.

Q3. What is componentDidMount used for?
componentDidMount is called once after the component is inserted into the DOM. It's ideal for side effects like fetching data, setting up subscriptions, or interacting with the DOM (e.g., initializing a third-party library).

Q4. What is componentWillUnmount used for?
componentWillUnmount is called just before the component is removed from the DOM. It's used for cleanup: canceling network requests, removing event listeners, or clearing timers to prevent memory leaks.

Q5. How do you handle lifecycle in functional components?
Functional components use the useEffect hook to handle side effects. useEffect can mimic componentDidMount, componentDidUpdate, and componentWillUnmount depending on the dependency array. For cleanup, you return a function from the effect.