Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 16090

Use the return value of a function as the src of a image tag in React

$
0
0

In my React project, I need to send a POST request to an API, the API will respond with an URL, and I want to use this URL as the src attribute of an image tag. The following is the conceptual work flow, which of course does not work. I wonder what is the correct way to write the code. Any help are highly appreciated!

const handlePostRequest = async () => {    try {        const response = await fetch('https://api.com', {            method: 'POST',            body: '',            headers: {'Content-Type': 'application/json',            },        });        const responseData = await response.json();    } catch (error) {        console.error('Error during POST request: ', error.message);    }    return responseData;};ImgSrc = handlePostRequest();return (<img        key={innerIndex}        src={ImgSrc}        title={image.title}        className="m-3"        loading="lazy"        alt={category}            />);

Viewing all articles
Browse latest Browse all 16090

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>