react fetchasyn

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

import axios from 'axios'
export default function Fetch1() {
    const [state,setState]=useState([]);
        const [loading,setLoading]=useState(true);
    useEffect(()=>{
        async function getdata(){ 
        
            try{
                setLoading(false);
                   const resawait axios.get('https://jsonplaceholder.typicode.com/posts');
                   console.log(res.data);
                   setState(res.data);
            }
            catch(err){
           console.log(err);
           setLoading(null);
            }
    
        }
getdata();
    },[])
  return (
      //make the condition here....
    <div>{state.map((e,index)=><li key={index}>{e.title}{e.id}</li>)}</div>
  )
}

Comments

Popular posts from this blog

interview questions js[ Anurag Singh ProCodrr]

reactnative_creation