Languages
[Edit]
EN

React / Axios - GET request

3 points
Created by:
Frida-Timms
607

In this article, we would like to show you how to make Axios GET requests in React.

Note: it is required to add external library to run below example, so use:

npm install axios

Axios GET request example

In the example below, we use async-await, but you might as well do it on promises.

import React from 'react'
import axios from 'axios'

const App = () => {
    const handleClick = async () => {
        try {
            const response = await axios.get('https://jsonplaceholder.typicode.com/todos/4');
            if (response.status === 200) {
                console.log(response.data);
            } else {
                console.error('Status 200 expected.');
            }
        } catch(error) {
            console.error(error);
        }
    };
    return (
        <div>
            <button onClick={handleClick}>Click to send GET request</button>
        </div>
    );
};

export default App;

Note: {JSON} Placeholder provides free testing API that works locally when you copy it.

Online runnable example: codesandbox.io

 

References

  1. https://jsonplaceholder.typicode.com/
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

React - axios

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join