How to Fetch Data in Reactjs?

by Aug 16, 2023Uncategorized

Different ways to fetch data in Reactjs

  • Fetch Method
  • Async-Await
  • Axios Library

Fetching Data with Fetch Method in Reactjs

 

function app(){

useEffect(() => {

fetch(‘https://www.yoursite.com’)

.then(response => response.json())

.then(json => console.log(json))

},[]);

return(

<div> Fetching Data with Fetch Method </div>

);

}

Fetching Data with Async – Await Method in Reactjs

 

function app(){

useEffect(() => {

(async () => {

try{ const result = await axios.get(‘https://www.site.com’) console.log(result.data)

} catch(error){

console.error(error)

}

})

})

return(

<div> Fetch Data with Async – Await Method in Reactjs

)

}

Fetching Data with Axios Library in Reactjs

 

function app(){

useEffect(() => {

axios.get(‘https://www.site.com’)

.then((response) => console.log(response.data));

},[]);

return(

<div> Fetch Data with Axios Library in Reactjs </div>

)

}

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *