In which lifecycle method should you make API calls in a class component?
- componentDidMount
- componentWillUnmount
- render
- constructor
You should make API calls in the componentDidMount lifecycle method of a class component. This method is called once after the component has been mounted in the DOM, making it suitable for actions like fetching data from an API. The other options (componentWillUnmount, render, and constructor) are not the appropriate places for making API calls.
Loading...
Related Quiz
- Which of the following can be a potential pitfall when using Portals?
- What is a potential downside to overusing React.memo in your application?
- What is state in React?
- When building a protected route in React Router, what is a common strategy to determine if a user should be redirected to a login page?
- How to define constants in React?