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'

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>




No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment





MarkiMarta.com. Notes of web-specialist
Since 2009
18+