首页 > 解决方案 > 我如何显示 API 结果

问题描述

如何在 React 中显示以下 API 结果?

我用来从 url 获取 api 数据的代码

constructor(props) {
        super(props);
        this.state = { results: [] };
    }

    componentDidMount() {
           fetch('url', {
            method: 'GET',
            headers: {
                'api-key': 'api-key'  
            }
        })
            .then(results => results.json())
            .then(data => this.setState({ results: data }));
    }

但这给了我这条线的错误信息

TypeError:results.map 不是函数 Function.renderemployeeTable

我尝试用 this.state = { results: [] };下面的 api 数据替换这一行,并能够使用<h3 class="title">{results.data.Table[0].first_name}{results.Table[0].last_name}</h3>

标签: api

解决方案


您需要存储状态或将其作为道具和显示发送。

<div>{data.Table[0].first_name}</div>

推荐阅读