useEffect and load async data in React
I spent a lot of time to understand how to get async data from web-services and to set it to vars in React
I need to try to change 'fetch' to 'axios'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
const host = 'https://localhost:7245/api/MySrv/'; //returns the array of data const [items, setItems] = useState({}); useEffect( () => { async function sendRequest() { try { const response = await fetch(host); const json = await response.json(); setItems(json.map( x=> x.title)); } catch (err) { console.log(err) } }; sendRequest() }, []) |
To display use in 'return' this string:
<div>{JSON.stringify(items)}</div>