1import React from 'react';
2
3import axios from 'axios';
4
5export default class PersonList extends React.Component {
6 state = {
7 persons: []
8 }
9
10 componentDidMount() {
11 axios.get(`https://jsonplaceholder.typicode.com/users`)
12 .then(res => {
13 const persons = res.data;
14 this.setState({ persons });
15 })
16 }
17
18 render() {
19 return (
20 <ul>
21 { this.state.persons.map(person => <li>{person.name}</li>)}
22 </ul>
23 )
24 }
25}
26
1const axios = require('axios');
2
3// Make a request for a user with a given ID
4axios.get('/user?ID=12345')
5 .then(function (response) {
6 // handle success
7 console.log(response);
8 })
9 .catch(function (error) {
10 // handle error
11 console.log(error);
12 })
13 .then(function () {
14 // always executed
15 });
16
17// Optionally the request above could also be done as
18axios.get('/user', {
19 params: {
20 ID: 12345
21 }
22 })
23 .then(function (response) {
24 console.log(response);
25 })
26 .catch(function (error) {
27 console.log(error);
28 })
29 .then(function () {
30 // always executed
31 });
32
33// Want to use async/await? Add the `async` keyword to your outer function/method.
34async function getUser() {
35 try {
36 const response = await axios.get('/user?ID=12345');
37 console.log(response);
38 } catch (error) {
39 console.error(error);
40 }
41}