Skip to main content
InterviewHubPRO
Section Overview
Q365

Question 365 of 407

Part 9: React Hooks Deep Dive (262 - 316)

90%
React Core & Hooks (407 Qs)•Part 9: React Hooks Deep Dive (262 - 316)

How to make AJAX call and in which component lifecycle methods should I make an AJAX call?

mid LevelCode ExampleComponentsHooksLifecycle

You can use AJAX libraries such as Axios, jQuery AJAX, and the browser built-in fetch. You should fetch data in the componentDidMount() lifecycle method. This is so you can use setState() to update your component when the data is retrieved.

sql
For example, the employees list fetched from API and set local state:
jsx
    class MyComponent extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
          employees: [],
          error: null,
        };
      }

      componentDidMount() {
        fetch("https://api.example.com/items")
          .then((res) => res.json())
          .then(
            (result) => {
              this.setState({
                employees: result.employees,
              });
            },
            (error) => {
              this.setState({ error });
            }
          );
      }

      render() {
        const { error, employees } = this.state;
        if (error) {
          return <div>Error: {error.message}</div>;
        } else {
          return (
            <ul>
              {employees.map((employee) => (
                <li key={employee.name}>
                  {employee.name}-{employee.experience}
                </li>
              ))}
            </ul>
          );
        }
      }
    }