how to fetch data using api in react js with axios

Solutions on MaxInterview for how to fetch data using api in react js with axios by the best coders in the world

showing results for - "how to fetch data using api in react js with axios"
Luana
30 Oct 2017
1import React, { Fragment, useState, useEffect } from 'react';
2import axios from 'axios';
3 
4function App() {
5  const [data, setData] = useState({ hits: [] });
6  const [query, setQuery] = useState('redux');
7 
8  useEffect(() => {
9    const fetchData = async () => {
10      const result = await axios(
11        'https://hn.algolia.com/api/v1/search?query=redux',
12      );
13 
14      setData(result.data);
15    };
16 
17    fetchData();
18  }, []);
19 
20  return (
21    <Fragment>
22      <input
23        type="text"
24        value={query}
25        onChange={event => setQuery(event.target.value)}
26      />
27      <ul>
28        {data.hits.map(item => (
29          <li key={item.objectID}>
30            <a href={item.url}>{item.title}</a>
31          </li>
32        ))}
33      </ul>
34    </Fragment>
35  );
36}
37 
38export default App;
Frieda
26 Jul 2017
1const users = data.map(u =>
2                    <div>
3                    <p>{u.id}</p>
4                    <p>{u.name}</p>
5                    <p>{u.email}</p>
6                    <p>{u.website}</p>
7                    <p>{u.company.name}
8
9                    </div>
10                    )
11this.setState({users})
12
13
14import React, {Component} from 'react'
15import axios from '../../axios'
16
17export default class users extends Component {
18    constructor(props) {
19        super(props);
20        this.state = {
21            Users: []
22        };
23    }
24    getUsersData() {
25        axios
26            .get(`/users`, {})
27            .then(res => {
28                const data = res.data
29                console.log(data)
30                const users = data.map(u =>
31                    <div>
32                    <p>{u.id}</p>
33                    <p>{u.name}</p>
34                    <p>{u.email}</p>
35                    <p>{u.website}</p>
36                    <p>{u.company.name}</p>
37                    </div>
38                    )
39
40                    this.setState({
41                        users
42                    })
43
44            })
45            .catch((error) => {
46                console.log(error)
47            })
48
49    }
50    componentDidMount(){
51        this.getUsersData()
52    }
53    render() {
54
55        return (
56            <div>
57                {this.state.users}
58            </div>
59        )
60    }
61}
queries leading to this page
fetch data real time using axios reactreactjs render after axios call fetch datahow to fetch data with axios in react axios call for fetch the data reactreact component for axios fetchesreact query fetch data axios axios fetch data react componentdidmountaxios fetch api reactreact js functional fetch axiosreact axios fetch data examplehow to fetch data from api in react using axiosreact axios get data from databasehow to fetch object and array data using axios in reacthow to get response in json react axiosusing axios to fetch data react componentdidmountreact jsfetch data using axios from web apifetch request with axios reacthow to fetch api using axios in reactconsuming rest api in react with fetch and axiosfetch data by axios and set the data to state in react jsaxios fetch data reacthow to get data from server in react js axiosget data from api with axios reacthow to fetch data from api in react using axios in functional componentget data from axios response reactfetch data by axios react mediumreact fetch axios data from apifetch data from api using axios react jsfetch and manage data axios reactis using fetch or axios better for reactreactjs axios fetch url from fetched dataconsuming rest apis in react with fetch and axiosaxios or fetch reacthow to get data from api using axios in react jsfetch data from post using axios reactreturn dom after axios fetch result reacthow to fetch data using axios in reacthow to fetch data with function comp axios from backend to reactreact js get data from api axiosreact fetch data from api with axois examplefetch data react axiosusing axios to fetch data in reactusing axios to fetch data reactreact use axios instead of fetchreact axios fetchwhich is best axios or fetch in react jsget value from api response using axios in react jsfetching data axios reactreact axios fetch and render databest way to fetch data in reatc with axiosaxios data fetch in reactjsfetch from api react without axiosfetch or axios in reactfetch data in component using axios reactaxios get data from api reacthowto get api data in react by axiosaxios fetch in reactreact use fetch and axios togetherhow to fetch data using api in react js with axiosfetch api react axiosfetch api call in react jsaxios fetch api data and render in react nativereact axios post json datashould use fetch on reactjs or axiosreact get data with axiosfetch data from json file in react axioshow to get data from database in react using axiosapi fetching using axios in react jsfetch data using fetch api in react js axiosreact best way fetch or axiosreact fetch api axiosreact how to fetch data with axiosreactjs fetch data to display axiosfetch and axios call api in reactfetch api react using axiosreact simple data fetching with axiosreact native fetch data using axiosfetching datat with axios in extrnal function react 3faxios in react fetch json and showfetch data react using axioshow to fetch data from api in react axiosfetch and axios call api using json in reacthow to use axios to fetch tha data and show the data into the table in reactfetch api data in react native axios and render sample codeget data from api react axiosaxios fetch react specific datahow to fetch data with axios from backend to reactreact js fetch data from api using axioshow to fetch data using axios in reactfetch axios with reactreactjs axios fetch api examplehow to get data from api in react using axiosreactjs axios fetch url from responseget data with axios reactreactjs fetch data using axios from web apiaxios react fetchfetch and axios in reactfetch api by axios in react jsreact axios get data from apiaxios in react js with data fetchingin react axios or fetch 3freact axios get json datahow to fetch in reacty with axiosreact site where data is fetch with the help of axios and shown in the cardsreact fetch dat data with axios and return in a tablereactjs axios fetch dataaxios and fetch reactreact axios fetch examplehow to fetch data from a post req axios in reacthow to return data fetch from api using axios in react nativereact js functional fetch axioreact query fetch or axiosfetch https react js axiosfetching data in react with axioswhere to use fetch and axios in reactreact axios fetch datareact fetch api with axiosfetch data from api using axios reacthow to fetch local json data using axios in reactreact axios fetch apiget data from api to frontend in react with axiosfetching data with axios reactfetch data with axios reactfetch data in react component with axioshow to get data from axios response reactbest practice to handle fetching data from axios in reactfetch data using axios reacthow to get data without fetch and axios in reacthow to locallu fetch json data using axios in reactjsaxios syntax to fetch the data in react react js http request fetch and axos axios fetching data reactgetting data from axios get response reactreturn after axios fetch result reactaxios fetch react apifetching data with react axiosaxios or fetch react which is betterhow to display data from api in react js using axioshow to convert fetch into axios in reactfetch single value using axios in react jsfetch axios reactreact display something different while axios is fetchingget data from api axios reactaxios and react to fetch data from apihow to fetch data in a component using axiosaxios fetch reactaxios fetch request with react jsfetch and axios call api using json in reactfetching get api react using axiosfetch data from json using axios reactreactjs function fetch data to display axiosfetch data 2 axios request reacthow to fetch data from api using axios in react nativehow to get data using axios in reactwhy do you prefer axios to fetch in react 3efetch data in table axios reactreact get data from api axioshow to fetch data from api using axios in react function componentsreact axios get json data fetchreactjs axios fetch data from apifetch data using axios in reactfetch data axios reactreact fetch axioshow to fetch data from api using axios in reactfetch data by id using react js axiosaxios to fetch data in reacthow to fetch loca 3bajson data using axios in reactjsaxios fetch request reactwhy use axios and not fetchfetch using axios in react 3dfetch from api react without axios 27fetch and axios in reactjsfetching data from an api axios componentfetch by id using axios react jsfetch react axiosfetching response from api in react using fetch api axiosreactjs axios get data from apifetching api in react in axios do fetch with axios reactget data with axios in reacthow to locally fetch json data using axios in react jsreact fetch data from api axiosreact native fetch data from api using axios examplefetch data from api using axios method in react nativereact ftch data with axioshow to fetch limited records from api using axios in reacthow can we fetch data from api in using axios reactaxios o fetchfetch data using axios in react examplehow to get request data in react js in axioshow to fetch using axios in react with an api keyfetch data from api react js using axiosuse axios to fetch data from an endpoint in reacthow to fetch data using api in react js with axios