【问题标题】:How to import/add multiple images as per JSON key value in ReactJS?如何根据 ReactJS 中的 JSON 键值导入/添加多个图像?
【发布时间】:2020-12-26 02:29:53
【问题描述】:

我正在尝试根据 JSON 数据中的 JSON 键值添加多个图像

{"名称":"缩写"}-> {“阿拉巴马州”:“AL”, “阿拉斯加”:“AK”, “亚利桑那”:“AZ”, “阿肯色州”:“AR”, “加利福尼亚”:“加利福尼亚”, “科罗拉多”:“CO”,...}

我下载了图像,并根据 JSON 格式的值命名为 'al.png' 、 'ak.png' 、 'az.png' 。如何为我创建的特定卡片组件设置每个图像

    import  "./card.styles.css"

export const Card =(props)=>(
    <div className="card-container">
    <h1>{props.states.name}
       I am trying to add images here



     
 </h1></div>)"

This is how I am fetching all states name onto Card-list component-

componentDidMount() {
    fetch("https://worldpopulationreview.com/static/states/abbr-name-list.json").
    then((response) => response.json())
    .then(users=>this.setState({states:users}));

  }

  render() {
    return (
      <div className='App'>
      <CardList states={this.state.states}>
      </CardList>
        
      </div>```

【问题讨论】:

  • 这些图片在哪里?

标签: javascript html json reactjs


【解决方案1】:

如果您在public 文件夹中有这些图像,那么您的卡片组件应如下所示:

export const Card =(props)=>(
  <div className="card-container">
  <h1>{props.states.name}
     <img src={`${process.env.PUBLIC_URL}/images/${props.states.abbreviation.toLowerCase()}.png`} />
  </h1>
</div>
)

【讨论】:

    【解决方案2】:

    据我了解,您希望向各个卡片提供其数据?

    如果我是对的,在您的 CardList 组件中,

    users.map((user) => <Card><h1>{user.name}</h1><img src={user.image}</img></Card>
    

    【讨论】:

      猜你喜欢
      • 2014-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-10
      • 1970-01-01
      • 2019-04-22
      • 2020-12-11
      相关资源
      最近更新 更多