Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

fetch data from api in react

useEffect(() => {
    const url = "https://api.adviceslip.com/advice";

    const fetchData = async () => {
      try {
        const response = await fetch(url);
        const json = await response.json();
        console.log(json);
      } catch (error) {
        console.log("error", error);
      }
    };

    fetchData();
}, []);
Comment

Fetching data with React

// mycomponent.js
import React, { useEffect, useState} from 'react';
import axios from 'axios';

const MyComponent = () => {
  const [loading, setLoading] = useState(true);
  const [data, setData] = useState([])

  useEffect(() => {
    const fetchData = async () =>{
      setLoading(true);
      try {
        const {data: response} = await axios.get('/stuff/to/fetch');
        setData(response);
      } catch (error) {
        console.error(error.message);
      }
      setLoading(false);
    }

    fetchData();
  }, []);

  return (
    <div>
    {loading && <div>Loading</div>}
    {!loading && (
      <div>
        <h2>Doing stuff with data</h2>
        {data.map(item => (<span>{item.name}</span>))}
      </div>
    )}
    </div>
  )
}

export default MyComponent;
Comment

how to data fetch in react

import {useEffect,useState} from 'react'

const App = () => {
  const [data,setData] = useState([])

  useEffect(()=>{
    fetch('https://fakestoreapi.com/products').then(res => res.json()).then(data => {
      setData(data);
    }).catch(e=>console.log(e.message));
  },[])

   console.log(data);

  return (
    <div>
      {data.map(i => <p key={i.id}>{i.title}</p> )}
    </div>
  )
}

export default App
Comment

PREVIOUS NEXT
Code Example
Javascript :: password page javascript 
Javascript :: javascript empty array 
Javascript :: regex data 
Javascript :: jquery countdown timer 
Javascript :: how to convert string to sentence case in javascript 
Javascript :: how to get sum array in javascript 
Javascript :: javascript url pas array 
Javascript :: what is polyfills in angular 
Javascript :: initialize function in javascript 
Javascript :: dynamically change meta tags javascript 
Javascript :: aes 256 nodejs 
Javascript :: how to chunk a base 64 in javascript 
Javascript :: get background image url jquery 
Javascript :: fuse.js 
Javascript :: console.log json shopify 
Javascript :: css on javascript 
Javascript :: how to check if text input has spaces javascript 
Javascript :: window.location.href url.action parameters 
Javascript :: string to array angular 
Javascript :: foreach modify array javascript 
Javascript :: check array values equal js 
Javascript :: change the position of div using javascript 
Javascript :: javascripti remove int character from string 
Javascript :: disable VirtualizedLists should never be nested inside 
Javascript :: axios.interceptors.response.use 
Javascript :: express add delay 
Javascript :: js settimeout wait element 
Javascript :: date format french js 
Javascript :: reload page after form submit javascript 
Javascript :: leaflet control zoom on scrolling page 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =