简体   繁体   English

我如何使用 props & map function 使用 React 为带有徽标的卡片插入图像 url?

[英]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.我正在从事一个个人项目,其中 NFL 数据按团队显示。 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.我刚刚学习 React,想知道如何使用数组中的 props 和 map 图像 url 来显示多个 NFL 标志卡。 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:我已经严格使用 css、html 和 javascript 创建了一个类似的网站,但需要在 React 中进行,无论如何,这就是我所拥有的:

Home.js首页.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]

[2]: https://i.stack.imgur.com/KK0tw.jpg , except for all 32 NFL teams [2]: https://i.stack.imgur.com/KK0tw.jpg ,所有 32 支 NFL 球队除外

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.如果您想将一些数据作为 props 传递给负责显示每个团队信息的 Card 组件,那么这会是什么样子。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM