【问题标题】:Mapping Through an Array w/ React and Bootstrap通过带有 React 和 Bootstrap 的数组进行映射
【发布时间】:2020-01-02 03:55:21
【问题描述】:

我正在尝试使用引导程序将 JSON 从 API 调用映射到返回。我希望名称一一映射到所有列和行,直到显示所有数据。目前,它正在映射所有 3 列中的所有数据(名称)。

componentDidMount() {
    fetch(API + 'ts=' + date + 'apikey=' + pubKey + 'hash=' + hash)
      .then(res => res.json())
      .then(
        (result) => {
          this.setState({
            isLoaded: true,
            char: result.data.results,
          });
        },
        (error) => {
          this.setState({
            isLoaded: true,
            error
          });
        }

      )
  }


  render() {
    const { error, isLoaded, char } = this.state;
    return (
      char.map(char => 
        <Container>
          <Row>
          <Col md="4"> <h2>{char.name}</h2></Col>
          <Col md="4"> <h2>{char.name}</h2></Col>
          <Col md="4"> <h2>{char.name}</h2></Col>
          </Row>
        </Container>
      )
    )
  }
}

【问题讨论】:

  • “希望名称一一映射到所有列和行”。这是什么意思?
  • 包含 JSON 结果/结果数组以及您想要映射的属性可能会有所帮助。目前尚不清楚您的预期结果应该是什么。你想“一一”映射什么?

标签: javascript reactjs dictionary


【解决方案1】:

如果我理解正确,如果您尝试映射所有数据并希望在网格内显示它。 您应该执行以下操作。

render() {
    const { error, isLoaded, char } = this.state;
    return (
        <Container>
          <Row>
           { char.map(char => (<Col md="4"> <h2>{char.name}</h2></Col> ) }
          </Row>
        </Container>
    )
}

【讨论】:

  • 好像就是这样。谢谢 !!只是好奇您是否在网上找到有关此的文档?我一直在寻找解决方案,但找不到。
  • 我只是读懂了你的想法:P。我对网格系统和响应能力有一些经验。
  • 也请接受答案,这样其他用户就无需花费时间来寻找正确的解决方案。 :)
猜你喜欢
  • 2018-02-24
  • 1970-01-01
  • 2019-09-26
  • 1970-01-01
  • 2021-07-09
  • 2019-09-02
  • 2022-07-25
  • 2020-08-24
  • 2011-09-11
相关资源
最近更新 更多