Skip to content
the poor coder

Axios with React Hooks, “traditional” Promise syntax

Poor Man
Published
Reading time
2 min read
On this page

Using Axios with React Hooks and Traditional Promise Syntax#

If you're developing a React application, you may need to make API calls to retrieve data. Axios is a popular library used for making HTTP requests, and React Hooks allow you to use state and other React features without writing a class.

Using Axios with React Hooks#

To use Axios with React Hooks, you need to install the Axios package:

JavaScript
npm install axios

Then, you can import Axios and use it in a functional component:

JavaScript
import React, { useState, useEffect } from 'react';
import axios from 'axios';

function MyComponent() {
  const [data, setData] = useState([]);

  useEffect(() => {
    axios.get('/api/data')
      .then(response => setData(response.data))
      .catch(error => console.error(error));
  }, []);

  return (
    <ul>
      {data.map(item => (<li key={item.id}>{item.name}</li>))}
    </ul>
  );
}

export default MyComponent;

In this example, the useState hook is used to initialize the data state as an empty array. The useEffect hook is used to make an API call to retrieve data from the /api/data endpoint. When the component mounts, the useEffect hook runs and makes the API call using Axios. If the API call is successful, the response data is stored in the data state using the setData function. If the API call fails, the error is logged to the console.

Using Traditional Promise Syntax with Axios#

If you prefer to use traditional Promise syntax instead of React Hooks, you can use Axios in a similar way:

JavaScript
import axios from 'axios';

axios.get('/api/data')
  .then(response => console.log(response.data))
  .catch(error => console.error(error));

In this example, the axios.get function is called to make an API call to retrieve data from the /api/data endpoint. If the API call is successful, the response data is logged to the console. If the API call fails, the error is logged to the console.

Overall, using Axios with React Hooks or traditional Promise syntax can help simplify making API calls in your React application.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC