Pabs
June 5, 2022, 3:15pm
#1
In the react project I’m currently working on, I need to render an image. It’s an object in an array and it’s passed as props to the component.
Here are the code fragments.
NameItem.jsx (complete code)
import React from 'react'
import moment from 'moment'
export default function NameItem(props) {
return (
<div>
<li>
<p>< img src= {props.avatar} alt="Avatar"/> {props.name}</p>
<p>Name: {props.name}</p>
<p>City: {props.location}</p>
<p>Email: {props.email}</p>
<p>Birthday: {moment(props.dob).format('DD-MM-YYYY')}</p>
</li>
</div>
)
}
NameList.jsx (code fragment)
import React from 'react'
import NameItem from './NameItem'
export default function NameList() {
//object array
const nameList = [{
"name": { "first": "brad","last": "gibson"},
"location": {"city": "kilcoole",},
"email": "brad.gibson@example.com",
"dob": {"date": "1993-07-20T09:44:18.674Z","age": 26},
"picture": {"large": "https://randomuser.me/api/portraits/men/75.jpg"},
},
The output is like this,
The image doesn’t load.
Can you show the full code that shows where NameList
is being used? You must be destructuring the objects in the nameList
array somewhere and passed to NameList
as the data displayed is part of a nested object. My first guess is you have not properly destructured the object to get the image for the avatar, but without seeing all your code, I can not be for certain.
1 Like
Pabs
June 5, 2022, 3:54pm
#3
Thanks for the reply,
Here’s the code, if you need other .js files, please inform me.
import React from 'react'
import NameItem from './NameItem'
export default function NameList() {
//object array
const nameList = [{
"name": { "first": "brad","last": "gibson"},
"location": {"city": "kilcoole",},
"email": "brad.gibson@example.com",
"dob": {"date": "1993-07-20T09:44:18.674Z","age": 26},
"picture": {"large": "https://randomuser.me/api/portraits/men/75.jpg"},
},
{
"name": { "first": "Jane","last": "gomez"},
"location": {"city": "Chillie",},
"email": "brad.gibson@example.com",
"dob": {"date": "1993-07-20T09:44:18.674Z","age": 26},
"picture": {"large": "https://images.pexels.com/photos/20787/pexels-photo.jpg?auto=compress&cs=tinysrgb&h=350"},
},
{
"name": { "first": "Sopia","last": "gibson"},
"location": {"city": "kilcoole",},
"email": "brad.gibson@example.com",
"dob": {"date": "1993-07-20T09:44:18.674Z","age": 26},
"picture": {"large": "https://images.pexels.com/photos/20787/pexels-photo.jpg?auto=compress&cs=tinysrgb&h=350"},
},
]
//Arrow function to iterate rendering
const printArray = () => {
return(
nameList.map((oneName) => {
return(
<NameItem
name = { `${oneName.name.first} ${oneName.name.last}`}
location = {oneName.location.city}
email = {oneName.email}
dob = {oneName.dob.date}
avatar = {oneName.picture} />
)
}
)
);
}
return (
<React.Fragment>
<h1>Name list</h1>
<hr/>
<ul>
{printArray()}
</ul>
</React.Fragment>
);
}
type or paste code here
Hi Pabs,
It should be like (Mod Edit: SOLUTION REDACTED) as picture is an object
1 Like
Pabs
June 5, 2022, 4:07pm
#5
Thank you for the support. It solved my problem.
Change this above line into the following:
Mod Edit: SOLUTION REDACTED
It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.
We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.
1 Like