Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to fetch api in reactjs using axios

//fetch api by using axios library//


import React, { Fragment, useState, useEffect } from 'react';
import axios from 'axios';
 
function App() {
  const [data, setData] = useState({ hits: [] });
  const [query, setQuery] = useState('redux');
 
  useEffect(() => {
    const fetchData = async () => {
      const result = await axios(
        'https://hn.algolia.com/api/v1/search?query=redux',
      );
 
      setData(result.data);
    };
 
    fetchData();
  }, []);
 
  return (
    <Fragment>
      <input
        type="text"
        value={query}
        onChange={event => setQuery(event.target.value)}
      />
      <ul>
        {data.hits.map(item => (
          <li key={item.objectID}>
            <a href={item.url}>{item.title}</a>
          </li>
        ))}
      </ul>
    </Fragment>
  );
}
 
export default App;
Comment

how to fetch data from react using axios

useEffect(()=>{
    axios.get(`http://localhost:5000/products`).then((response) => {
        console.log(response);
        //setProducts(response.data)
      });
},)
Comment

fetch data with axios in reactjs

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

const View = _ => {
    const [posts, setPosts] = useState([])

    const url = "https://mrcrudapp.herokuapp.com/api/getallposts"

    const postings = async () => {
        try{
            const res = await axios.get(url)
            setPosts(res.data.data)
        } catch (error) {
            console.log('error')
        }
    }

    useEffect( () =>  postings(), [])

    return (
        <>
        <h1>Posts</h1>
        { posts && posts.map((myPost) =>
            <ul key={myPost._id}>
                <li>{myPost.title} : {myPost.content}</li>
            </ul>
        )}
        </>
    )
}

export default View
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript beforeunload 
Javascript :: node uuid 
Javascript :: destructure dynamic properties 
Javascript :: scrollview refresh 
Javascript :: random string js 
Javascript :: jquery get mouse page left and top 
Javascript :: neo4j create relationship between existing nodes 
Javascript :: javascript date get future 15 minutes 
Javascript :: npm redux toolkit 
Javascript :: javascript current date time 
Javascript :: check if input is a number javascript 
Javascript :: Capitalise a String 
Javascript :: Shuffle a Sting in JavaScript 
Javascript :: time calculator js 
Javascript :: js classlist 
Javascript :: how to loop through array of numbers in javascript 
Javascript :: run nextjs in separate port 
Javascript :: regex match word inside string 
Javascript :: react native android safeareaview 
Javascript :: how to stop server of react js 
Javascript :: socket.io reconnect example 
Javascript :: check device in flutter 
Javascript :: check undefined in javascript 
Javascript :: input onenter go to next input field javascript 
Javascript :: Vue use props in style 
Javascript :: find element by two attributes jquery 
Javascript :: add char in specific index stirng javascript 
Javascript :: write to console using jQuery 
Javascript :: convert array of string to array of objects javascript 
Javascript :: javascript async fetch file html 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =