Lifecycle Methods in React Native
Lifecycle Methods in React Native Interview with follow-up questions
1. What are lifecycle methods in React Native?
Lifecycle methods are hooks into the stages a component goes through: mounting (created and inserted), updating (re-rendered after props/state change), and unmounting (removed). They let you run code at the right moment — initialize state, fetch data, subscribe to events, or clean up resources.
In 2026 this question is really testing whether you know that class lifecycle methods are legacy. Modern React Native uses function components and Hooks, where useEffect covers the lifecycle:
useEffect(() => {
// runs after mount (and after updates if deps change)
const sub = subscribe();
return () => sub.unsubscribe(); // runs before unmount / before next effect
}, [deps]);
So the mapping is: componentDidMount + componentDidUpdate + componentWillUnmount ≈ useEffect. The old componentWillMount / componentWillReceiveProps / componentWillUpdate methods are deprecated (renamed UNSAFE_*, legacy names removed back in React 17) — don't reach for them. Class lifecycle still exists for error boundaries (componentDidCatch, getDerivedStateFromError), which Hooks can't yet fully replace.
Follow-up 1
Can you explain the sequence in which these methods are called?
The sequence in which lifecycle methods are called in React Native is as follows:
- constructor: This is called when the component is first created. It is used to initialize state and bind event handlers.
- render: This method is called to render the component's UI.
- componentDidMount: This is called after the component has been rendered to the screen. It is commonly used to fetch data from an API or set up subscriptions.
- componentDidUpdate: This is called when the component's props or state have changed. It is used to perform side effects or update the component's UI.
- componentWillUnmount: This is called when the component is about to be removed from the screen. It is used to clean up any resources or subscriptions.
Follow-up 2
What is the significance of each lifecycle method?
Each lifecycle method has a specific significance:
- constructor: It is used to initialize state and bind event handlers.
- render: It is responsible for rendering the component's UI.
- componentDidMount: It is commonly used to fetch data from an API or set up subscriptions.
- componentDidUpdate: It is used to perform side effects or update the component's UI.
- componentWillUnmount: It is used to clean up any resources or subscriptions before the component is removed from the screen.
Follow-up 3
How are lifecycle methods used in class components?
In class components, lifecycle methods are defined as class methods. They are automatically called by React Native at different stages of the component's life cycle. To use a lifecycle method, you simply define it in your class component and React Native will invoke it at the appropriate time.
Follow-up 4
Can you give an example of a lifecycle method in use?
Sure! Here's an example of the componentDidMount lifecycle method in use:
import React, { Component } from 'react';
class MyComponent extends Component {
componentDidMount() {
// Fetch data from an API
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => {
// Update component state with fetched data
this.setState({ data });
});
}
render() {
// Render component UI
return (
<div>
{/* Render component content */}
</div>
);
}
}
2. What is the difference between componentWillMount and componentDidMount?
componentDidMount runs once, right after the component is first rendered and inserted — it's the correct place for side effects like data fetching, subscriptions, or timers, because the component is on screen and setState here triggers a re-render safely.
componentWillMount ran before the first render. It's deprecated — renamed UNSAFE_componentWillMount and removed under its legacy name in React 17. It was never safe for async work (the result wasn't guaranteed before paint) and breaks with concurrent rendering, so a good 2026 answer is: don't use it.
In modern function components both collapse into useEffect:
useEffect(() => {
const controller = new AbortController();
fetch(url, { signal: controller.signal })
.then(/* ... */);
return () => controller.abort(); // cleanup on unmount
}, []); // empty deps = run once, like componentDidMount
Gotcha interviewers probe: anything you'd have put in componentWillMount (e.g. setting initial state) belongs in the useState initializer or the component body, not in an effect.
Follow-up 1
In what scenarios would you use each of these methods?
You would use the componentWillMount method when you need to perform some setup tasks before the component is rendered, such as initializing state or making API calls. On the other hand, you would use the componentDidMount method when you need to perform side effects after the component has been rendered, such as fetching data from an API or subscribing to events.
Follow-up 2
What are the potential issues of using componentWillMount?
There are a few potential issues with using the componentWillMount method. First, it is called on both the server and the client, which can lead to inconsistencies if the code inside the method assumes a specific environment. Second, since it is called before the component is rendered, any state updates made inside this method will not trigger a re-render. This can lead to unexpected behavior if the state is updated based on props or other external factors. Lastly, the componentWillMount method is considered legacy and may be deprecated in future versions of React.
Follow-up 3
How does React Native handle these methods in the latest versions?
In the latest versions of React Native, the componentWillMount method is still available for use, but it is recommended to use the constructor or componentDidMount methods instead. This is because the componentWillMount method may be deprecated in future versions. The componentDidMount method works the same way as in React, and is commonly used for performing side effects after the component has been rendered.
3. What is the purpose of the render method in React Native?
In a class component, render() returns the JSX (the element tree) describing what the UI should look like for the current props and state. React calls it on mount and on every update. It must be a pure function of props and state — no side effects, no setState, no subscriptions inside it — so React can call it freely and, under concurrent rendering, even discard and re-run it.
In function components (the modern default), there is no render method — the function body itself is the render. Whatever you return is the equivalent:
function Profile({ name }) {
return {name}; // this return IS the render
}
The same purity rule applies: keep side effects out of the body and put them in useEffect. Note RN's render returns native-backed components (,), not HTML — React reconciles the returned tree and Fabric commits the result to native views.
Follow-up 1
What happens if the render method is not defined?
If the render method is not defined in a React Native component, an error will occur. The render method is mandatory in React components as it determines what should be displayed on the screen.
Follow-up 2
How often is the render method called in a component's lifecycle?
The render method is called whenever the component needs to be re-rendered. This can happen due to changes in the component's state or props. However, React optimizes the rendering process and may not always re-render the component if there are no changes in its state or props.
Follow-up 3
Can you modify the state or props inside the render method?
It is not recommended to modify the state or props inside the render method. The render method should be a pure function, meaning it should not have any side effects. Modifying the state or props inside the render method can lead to unexpected behavior and can cause an infinite loop of re-rendering.
4. Can you explain the componentDidUpdate method?
componentDidUpdate(prevProps, prevState) is a class lifecycle method called immediately after a re-render caused by a props or state change (it does not run on the initial mount). It's used for side effects that depend on the update — fetching new data when an ID prop changes, or imperatively reacting to a value.
The classic gotcha interviewers look for: if you setState here you must guard it with a comparison, or you cause an infinite render loop:
componentDidUpdate(prevProps) {
if (prevProps.userId !== this.props.userId) {
this.fetchUser(this.props.userId);
}
}
In React Native there is no DOM — the "update" commits to native views via Fabric, not HTML. In modern function components, the equivalent is useEffect with a dependency array, where the deps replace the manual prevProps comparison:
useEffect(() => {
fetchUser(userId);
}, [userId]); // runs only when userId changes
Follow-up 1
When is componentDidUpdate called?
The componentDidUpdate method is called after an update occurs to the component's state or props. It is not called during the initial render of the component. It is called whenever the component's state or props are updated, either by calling setState or by receiving new props from its parent component.
Follow-up 2
What are the arguments passed to this method?
The componentDidUpdate method receives two arguments: prevProps and prevState. These arguments represent the previous props and state of the component before the update occurred. By comparing the current props and state with the previous props and state, you can determine what has changed and perform any necessary actions or updates.
Follow-up 3
Can you give an example where you would use componentDidUpdate?
Sure! Here's an example where componentDidUpdate is used to fetch new data from an API when the component's props change:
class MyComponent extends React.Component {
componentDidUpdate(prevProps) {
if (this.props.userId !== prevProps.userId) {
this.fetchData();
}
}
fetchData() {
// Fetch data from API based on the new props
}
render() {
// Render component
}
}
In this example, the componentDidUpdate method is used to compare the current userId prop with the previous userId prop. If they are different, the fetchData method is called to fetch new data from the API based on the new prop value.
5. What is the componentWillUnmount method used for?
componentWillUnmount is a class lifecycle method called right before a component is removed and destroyed. It's your cleanup hook: cancel network requests, remove event listeners, clear timers/intervals, and unsubscribe from stores or sockets — failing to do so causes memory leaks and the classic "can't update state on an unmounted component" warning.
In modern function components, cleanup is the return function of useEffect:
useEffect(() => {
const id = setInterval(tick, 1000);
const sub = navigation.addListener('focus', onFocus);
return () => {
clearInterval(id); // runs on unmount (and before re-running the effect)
sub();
};
}, []);
Gotcha: the useEffect cleanup runs before every re-run of the effect, not only on unmount — so with non-empty deps it fires more often than componentWillUnmount would. For abortable fetches, use AbortController in the cleanup.
Follow-up 1
What kind of operations are suitable to perform in componentWillUnmount?
In componentWillUnmount, you can perform any necessary cleanup operations such as:
- Cancelling network requests
- Removing event listeners
- Clearing timers or intervals
- Releasing resources or subscriptions
- Cleaning up any external dependencies
It is important to perform these cleanup operations to prevent memory leaks and ensure the component is properly unmounted.
Follow-up 2
What happens if componentWillUnmount is not defined?
If componentWillUnmount is not defined in a component, there will be no specific cleanup operations performed when the component is unmounted. This can lead to memory leaks or other unexpected behavior if the component has any ongoing operations or subscriptions that need to be cleaned up. It is generally recommended to define componentWillUnmount and handle any necessary cleanup operations.
Follow-up 3
Can you give an example where you would use componentWillUnmount?
Sure! Here's an example where componentWillUnmount is used to remove an event listener:
import React, { Component } from 'react';
class MyComponent extends Component {
componentDidMount() {
window.addEventListener('resize', this.handleResize);
}
componentWillUnmount() {
window.removeEventListener('resize', this.handleResize);
}
handleResize = () => {
// Handle resize event
}
render() {
return (
<div>
{/* Component content */}
</div>
);
}
}
6. Explain the useEffect hook — its dependency array, cleanup, and common pitfalls.
useEffect runs side effects (data fetching, subscriptions, timers, manual DOM/native calls) after render. It's the function-component replacement for componentDidMount/componentDidUpdate/componentWillUnmount.
useEffect(() => {
const sub = api.subscribe(id, setData);
return () => sub.unsubscribe(); // cleanup: runs before next effect + on unmount
}, [id]); // re-runs only when `id` changes
The dependency array controls when it re-runs:
[]→ run once after mount.[a, b]→ run after mount and wheneveraorbchange.- omitted → run after every render (usually a bug).
Common pitfalls interviewers probe:
- Missing dependencies → stale closures reading old state/props (enable the
react-hooks/exhaustive-depslint rule). - Infinite loops → setting state in an effect whose dependency is that state, or passing a new object/array/function literal each render (memoize with
useMemo/useCallback). - Forgetting cleanup → leaked listeners/timers and "setState on unmounted component" warnings.
- Using it for work that belongs during render (derive values instead) — the React team's guidance is "you might not need an effect."
Live mock interview
Mock interview: Lifecycle Methods in React Native
- Read your scene and goals
- Talk it out; goals tick off live
- Get a score and stronger lines
Your voice and your AI key never touch our servers; the key stays in this browser and is sent only to Google. Only your round scores are saved to track progress.