带有REACT JS的FETCH API-您可以帮助我吗?

问题描述 投票:-3回答:1

enter image description here

我对本地api网址有疑问,我使用slim做我的api

reactjs api slim fetch-api
1个回答
0
投票

我看到您没有以您的状态存储api结果。我怀疑这可能是问题所在。请检查下面的代码,它可能会做您想要的事情。

componentDidMount() {
fetch('api/public/all').
    then(res=>res.json()).
    then(parsedJson => {
        const items = parsedJson.result.map(data => ({
            id: `${data.id}`,
            firstName: `${data.first_name}`,
            lastName: `${data.last_name}`,
            country: `${data.country}`,
            position: `${data.position}`
        }));
        this.setState({ items });
    })
}
© www.soinside.com 2019 - 2024. All rights reserved.