PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 13, 2022

[FIXED] How to get an image link from a JSON to render in a react app

 July 13, 2022     javascript, reactjs, web-deployment     No comments   

Issue

new to react here. As the title says, I have a JSON that has data in it that I am trying to render. Everything is showing except for the pictures. How can I make them show. Here is my code:

import React, { useEffect, useState } from 'react';
import './App.css';

function App() {
  const [users, setUsers] = useState([]);

  useEffect(() => {
    fetch('https://reqres.in/api/users')
    .then(response => response.json())
    .then(resData => setUsers(resData.data))
  }, []);

  return (
    <div className="App">
      <table>
        <tbody>
        {
          users.map((user, index) => 
           <tr key={index}>
             <td>{user.first_name}</td>
             <td>{user.last_name}</td>
             <td>{user.email}</td>
             <td>{user.avatar}</td>
           </tr>
          )
        }
        </tbody>
      </table>
     </div>
  );
}

export default App;

The end result should be like this: https://i.imgur.com/FV4B6fg.png


Solution

You can use an img tag to render user avatar,

 <td>
  <img src={user.avatar} />
 </td>


Answered By - Abito Prakash
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing