I am working on a personal project where NFL Data is displayed by team. I am just learning React and would like to know how to use props and map image urls from an array to display multiple NFL logo cards. I have made a similar website using strictly css, html, and javascript but need to do it in react, anyways, this is what I have:
Home.js
JavaScript
x
47
47
1
import React from "react"
2
import { Link} from "react-router-dom"
3
import Box from '@material-ui/core/Box';
4
5
const teams = [
6
{
7
id: 1,
8
teamName: "Kansas City Cheifs",
9
urlImage: "public/chiefs_logo.jpg"
10
},
11
{
12
id: 2,
13
teamName: "Cincinatti Bengals",
14
urlImage: "public/Bengals.jpg"
15
16
},
17
{
18
id: 3,
19
teamName: "Denver Broncos",
20
urlImage: "public/Denver-Broncos-symbol.jpeg"
21
22
},
23
{
24
id: 4,
25
teamName: "Carolina Panthers",
26
urlImage: "public/panthers.png"
27
28
}
29
];
30
31
32
33
34
export default function Home(props) {
35
36
return (
37
<div className="Team-Box">
38
const teamCards = teams.map(team => )
39
<Box className="Box" key={teams.id} background-image={props.urlImage}/>
40
<Box className="Box" background-image={props.urlImage}/>
41
<Link to="/Home"></Link>
42
</div>
43
44
45
)
46
}
47
[What I want it to look like][2]
[2]: https://i.stack.imgur.com/KK0tw.jpg, except for all 32 NFL teams
Advertisement
Answer
Inside of your return you want something like this.
JavaScript
1
14
14
1
return (
2
<div>
3
{teams.map((team) => (
4
<div key={team.id} className='Team-Box'>
5
<Box
6
className='Box'
7
style={{ backgroundImage: `url(${team.imageUrl})` }}
8
/>
9
</div>
10
))}
11
<Link to='/Home'></Link>
12
</div>
13
);
14
Here is an idea of what this would look like if you wanted to pass some data as props to a Card component responsible for displaying the information on each team.
JavaScript
1
45
45
1
import { useState } from 'react';
2
3
const initialTeams = [
4
{
5
id: 1,
6
teamName: 'Kansas City Chiefs',
7
urlImage: 'public/chiefs_logo.jpg',
8
},
9
{
10
id: 2,
11
teamName: 'Cincinatti Bengals',
12
urlImage: 'public/Bengals.jpg',
13
},
14
{
15
id: 3,
16
teamName: 'Denver Broncos',
17
urlImage: 'public/Denver-Broncos-symbol.jpeg',
18
},
19
{
20
id: 4,
21
teamName: 'Carolina Panthers',
22
urlImage: 'public/panthers.png',
23
},
24
];
25
26
const Card = ({ imageUrl, teamName }) => (
27
<div className='team-card' style={{ backgroundImage: `url(${imageUrl})` }}>
28
{teamName}
29
</div>
30
);
31
32
const Home = () => {
33
const [teams, setTeams] = useState(initialTeams);
34
35
return (
36
<div>
37
{teams.map(({ id, imageUrl, teamName }) => (
38
<Card key={id} imageUrl={imageUrl} teamName={teamName} />
39
))}
40
</div>
41
);
42
};
43
44
export default Home;
45