简体   繁体   English

如何获取API数据并在react.js中以表格形式显示

[英]How to fetch API data and show that on table form in react.js

Here is my code I'm not getting how to show data on the page. 这是我的代码,我没有得到如何在页面上显示数据的信息。 When I tried I got nothing on the page and it shows: 当我尝试时,页面上什么也没有,它显示:

Warning: Each child in an array or iterator should have a unique "key" prop. 警告:数组或迭代器中的每个子代都应具有唯一的“键”道具。

Here is my Code. 这是我的代码。

import React, { Component } from 'react';
import './App.css';

class App extends Component {
  constructor(){
    super();
      this.state = {
        data: []
        }
    }

    componentDidMount() 
    {
      fetch("https://blockchain.info/ticker").
        then((Response) => Response.json()).
          then ((findresponse)=>
            {
              console.log(findresponse)
              this.setState({
                data: [findresponse]
                  });
            }) 
    }

    render() 
    {
      return(
        <div>
          {
            this.state.data.map((dynamicData, Key) =>
              <div>
                <span>{dynamicData.key}</span>
              </div>
            )
          }
        </div>
        )
    }
}

export default App;

This is a react warning. 这是一个反应警告。

As per the docs : 根据文档

A “key” is a special string attribute you need to include when creating lists of elements. “键”是创建元素列表时需要包括的特殊字符串属性。

You are having an issue accessing the data returned from bitcoin as you have an array of objects. 当您有一系列对象时,在访问从比特币返回的数据时遇到问题。 To access the data being returned from the API you need to do something like the following: 要访问从API返回的数据,您需要执行以下操作:

 class Example extends React.Component { constructor() { super(); this.state = { data: [] } } componentDidMount() { fetch("https://blockchain.info/ticker"). then(response => response.json()). then(findresponse => { this.setState({ data: [findresponse] }); }) } render() { return ( <div> { this.state.data.map((dynamicData, Key) => { let keys = Object.keys(dynamicData); let d = dynamicData; return keys.map(data => { return ( <div style={{borderBottom: '1px solid black'}}> <p>Currency: {data}</p> <p>Buy: {dynamicData[data].buy}</p> </div> ); }); }) } </div> ) } } ReactDOM.render(<Example />, document.getElementById("root")); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="root"></div> 

Is there anything wrong at all with your code or is it just a question about the warning message? 您的代码有什么问题吗?还是仅是有关warning消息的问题? If that's the case, whenever you map through an array in React, you have to give them a key prop. 如果是这样,每当您在React中通过数组映射时,都必须给他们一个key道具。 This is helpful so React knows which items have changed, updated or even added in the future. 这很有用,因此React知道将来会更改,更新甚至添加了哪些项目。

// Key here stands for dynamicData's index in the array and not its key prop.
this.state.data.map((dynamicData, Key) =>
  <div key={Key}>
    <span>{dynamicData.key}</span>
  </div>
)

I suggest you to rename it from Key to index so you don't confuse keys within your code. 我建议您将其从Key重命名为index以免混淆代码中的键。

this.state.data.map((dynamicData, index) =>
  <div key={index}>
    <span>{dynamicData.key}</span>
  </div>
)

For more information about keys in React, I suggest you to read this link. 有关React中的keys更多信息,建议您阅读链接。

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

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