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 :: js clearect 
Javascript :: make an object javascript 
Javascript :: swap numbers in javascript 
Javascript :: validar array vacio javascript 
Javascript :: Reusable Alpine.js components 
Javascript :: p5js left mouse click 
Javascript :: Remove duplicate items in an array 
Javascript :: moment clone 
Javascript :: remove trailing zeros javascript 
Javascript :: card react native 
Javascript :: get text selected 
Javascript :: canvas set image height 
Javascript :: javascript add element to serialized form array 
Javascript :: javascript jquery map a range of numbers 
Javascript :: render first index active tabs in reactjs 
Javascript :: javascript things to remember 
Javascript :: remove duplicates in array 
Javascript :: sum range javascript 
Javascript :: how to check what browser you are using 
Javascript :: types for parameter destructuring 
Javascript :: refresh a single component 
Javascript :: csrf javascript 
Javascript :: js group objects in array 
Javascript :: js loop through function arguments 
Javascript :: react leaflet disable zoom 
Javascript :: update array of objects with use state 
Javascript :: formik validate field array select 
Javascript :: javascript array from string 
Javascript :: how to push two values in array at once 
Javascript :: Split string into words, without punctuation 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =