Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use a image file from a API?

I’m doing a NFT page and I’m trying to use some img from an api but it seems it returns the file image itself so how can I transform or get that path to give me the actual img.

Here is my code:

const [userData, setUserData] = useState([])

async function fetchData() {
    const { data } = await axios.get('https://us-central1-nft-cloud-functions.cloudfunctions.net/hotCollections')
    setUserData(data) 
    console.log(data)
} 

useEffect(() => {
    fetchData()
}, [])

I’m searching but I havent found a solution yet.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It looks like the data you’re receiving is the exact src value for an <img> element. For example, when I go to your URL, the first object in the returned array is:

{
  "id": 1,
  "title": "Abstraction",
  "authorImage": "https://nft-place.web.app/static/media/author-1.04ee784f53cbe427d362.jpg",
  "nftImage": "https://nft-place.web.app/static/media/coll-1.b8f9d867e8ed59ee7fa7.jpg",
  "nftId": 39924405,
  "authorId": 83937449,
  "code": 192
}

Those image values are simply URLs (or in some cases base64 image data, but to the browser it makes little difference), which you can use in your markup. For example:

<>
{
  userData.map(user => (
    <div key={user.id}>
      <h1>{user.title}</h1>
      <img src={user.authorImage} alt={user.title} />
    </div>
  ))
}
</>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading