skip to Main Content

Javascript – What if setState is used to fetch API?

I was experimenting with react. and used setState as shown below. import {useState} from 'react'; export default function App() { const [state,setState]=useState([]); const fetchApi= async()=>{ return fetch('https://jsonplaceholder.typicode.com/posts') .then(response=>response.json()) .then(data=>{console.log(data); return data}) .catch(err=>console.log(err)) } setState(fetchApi()); return ( <div> </div> ); }…

VIEW QUESTION
Back To Top
Search