简体   繁体   English

为什么我的图像没有映射到我的 React 组件?

[英]Why isn't my image being mapped to my React Component?

The title seems to be mapped fine but the images are not.标题似乎映射得很好,但图像不是。 I went over the code a couple of times but I can't find out what's wrong.我检查了代码几次,但找不到问题所在。 Let me know what changes I have to make.让我知道我必须做出哪些改变。 The author of the tutorial did use class based components and I converted it to functional components but regardless the code should display the images but it isn't.本教程的作者确实使用了基于 class 的组件,我将其转换为功能组件,但无论代码是否应该显示图像,但事实并非如此。

// directory-data.jsx

export const sections = [
  {
    title: 'hats',
    imageUrl: 'https://i.ibb.co/cvpntL1/hats.png',
    id: 1,
    linkUrl: 'shop/hats',
  },
// menu-item.component.jsx

import React from 'react'
import './menu-item.styles.scss'
const MenuItem = ({ title, imageUrl }) => (
  <div
    style={{
      backgroundImage: `url(${imageUrl})`,
    }}
    className='menu-item'
  >
    <div className='content'>
      <h1 className='title'>{title}</h1>
      <span className='subtitle'>Shop Now</span>
    </div>
  </div>
)

export default MenuItem

// directory.component.jsx

import React from 'react'
import { sections } from './directory-data'
import MenuItem from '../menu-item/menu-item.component'
import './directory.styles.scss'

export const Directory = () => {
  return (
    <div className='directory-menu'>
      {sections.map(({ title, imageURL, id }) => (
        <MenuItem key={id} title={title} imageUrl={imageURL} />
      ))}
    </div>
  )
}
// homepage.component.jsx

import React from 'react'
import './hompage.styles.scss'
import { Directory } from '../../components/directory/directory.component'

const Homepage = () => (
  <div className='homepage'>
    <Directory />
  </div>
)

export default Homepage

Your object looks like this:您的 object 如下所示:

{
 title: 'hats',
 imageUrl: 'https://i.ibb.co/cvpntL1/hats.png',
 id: 1,
 linkUrl: 'shop/hats',
},

as you can see the property name is imageUrl如您所见,属性名称是imageUrl

in your map function you are trying to take a property named imageURL在您的 map function 中,您正在尝试获取名为imageURL的属性

{
   sections.map(({ title, imageURL, id }) => (
    <MenuItem key={id} title={title} imageUrl={imageURL} />
 ))}

the fix is change the imageURL to imageUrl when you deconstruct the object解决方法是在解构imageUrl时将imageURL更改为 imageUrl

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

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