Skip to content
Advertisement

How do i use props & map function to insert image urls for cards with logos using React?

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

import React from "react"
import { Link} from "react-router-dom"
import Box from '@material-ui/core/Box';

const teams = [
    {
        id: 1,
        teamName: "Kansas City Cheifs",
        urlImage: "public/chiefs_logo.jpg"
    },
    {
        id: 2,
        teamName: "Cincinatti Bengals",
        urlImage: "public/Bengals.jpg"
      
    },
    {
        id: 3,
        teamName: "Denver Broncos",
        urlImage: "public/Denver-Broncos-symbol.jpeg"
        
    },
    {
        id: 4,
        teamName: "Carolina Panthers",
        urlImage: "public/panthers.png"
       
    }
  ];




export default function Home(props) {

    return (
        <div className="Team-Box">
            const teamCards = teams.map(team => )
            <Box className="Box" key={teams.id} background-image={props.urlImage}/>
            <Box className="Box"  background-image={props.urlImage}/>
        <Link to="/Home"></Link>
        </div>
            
        
    )
}

What it looks like so far

[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.

return (
  <div>
    {teams.map((team) => (
      <div key={team.id} className='Team-Box'>
        <Box
          className='Box'
          style={{ backgroundImage: `url(${team.imageUrl})` }}
        />
      </div>
    ))}
    <Link to='/Home'></Link>
  </div>
);

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.

import { useState } from 'react';

const initialTeams = [
  {
    id: 1,
    teamName: 'Kansas City Chiefs',
    urlImage: 'public/chiefs_logo.jpg',
  },
  {
    id: 2,
    teamName: 'Cincinatti Bengals',
    urlImage: 'public/Bengals.jpg',
  },
  {
    id: 3,
    teamName: 'Denver Broncos',
    urlImage: 'public/Denver-Broncos-symbol.jpeg',
  },
  {
    id: 4,
    teamName: 'Carolina Panthers',
    urlImage: 'public/panthers.png',
  },
];

const Card = ({ imageUrl, teamName }) => (
  <div className='team-card' style={{ backgroundImage: `url(${imageUrl})` }}>
    {teamName}
  </div>
);

const Home = () => {
  const [teams, setTeams] = useState(initialTeams);

  return (
    <div>
      {teams.map(({ id, imageUrl, teamName }) => (
        <Card key={id} imageUrl={imageUrl} teamName={teamName} />
      ))}
    </div>
  );
};

export default Home;
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement