2021年3月7日星期日

Save a react axios response of a .jpg into state then display that image from state

How can I save the response data into state first directly from any .jpg link then load that image into the img tag.

import { useState, useEffect } from 'react';  import axios from 'axios';    const ProfileImage = () => {      const [profileImage, setProfileImage] = useState();    const [loading, setLoading] = useState(true);        const getUserImage = async () => {      let res = await axios({        method: 'get',        url: `https://st.depositphotos.com/1937573/2310/i/600/depositphotos_23101854-stock-photo-handsome-man-outdoor.jpg`      });      setProfileImage(res.data);      setLoading(false);    }        useEffect(() => {      getUserImage();      }, []);      return (        {loading && <div>loading...</div>}        {!loading && <img src={profileImage} />}    )  };    export default ProfileImage;
https://stackoverflow.com/questions/66523941/save-a-react-axios-response-of-a-jpg-into-state-then-display-that-image-from-st March 08, 2021 at 11:23AM

没有评论:

发表评论