[英]How do I access the properties of API in my react application?
I am new to web development, and cant figure out how to access the properties of my API.我是 Web 开发的新手,无法弄清楚如何访问我的 API 的属性。 The API I am using provides with me the information of football teams.
我正在使用的 API 为我提供了足球队的信息。 Given is the Schema and link of the API: https://rapidapi.com/api-sports/api/api-football?endpoint=apiendpoint_bc5e37ef-299f-4656-98a3-ed0d9fa5b1d2
给出了 API 的架构和链接: https : //rapidapi.com/api-sports/api/api-football?endpoint=apiendpoint_bc5e37ef-299f-4656-98a3-ed0d9fa5b1d2
Given is my App.js code for the react application.给定的是我用于反应应用程序的 App.js 代码。
import React from "react";
class App extends React.Component {
constructor() {
super();
this.state = {
teamObj:{}
};
}
componentDidMount() {
fetch("https://api-football-v1.p.rapidapi.com/v2/teams/team/33",
{"method": "GET","headers":
{"x-rapidapi-host": "api-football-v1.p.rapidapi.com",
"x-rapidapi-key": "a35b8572b7mshe694b9e8ac66df4p158c4bjsn4518b48e8965"}
}
)
.then(response => response.json())
.then(data => this.setState({teamObj : data}))
}
render() {
return (
<div>
<h1>My fav team is: {this.state.teamObj.name}</h1>
</div>
);
}
}
export default App;
import React from 'react';
class App extends React.Component {
constructor() {
super();
this.state = {
teamObj: {}
};
}
componentDidMount() {
fetch('https://api-football-v1.p.rapidapi.com/v2/teams/team/33', { method: 'GET', headers: { 'x-rapidapi-host': 'api-football-v1.p.rapidapi.com', 'x-rapidapi-key': 'a35b8572b7mshe694b9e8ac66df4p158c4bjsn4518b48e8965' } })
.then((response) => response.json())
.then((data) => this.setState({ teamObj: data }));
}
render() {
const data = this.state.teamObj;
if (!data) return <div>Loading...</div>;
const teamData = data.api && data.api.teams && data.api.teams.length > 0 ? data.api.teams : [];
return (
<div>
<h1>
My fav teams is:{' '}
{teamData.map((x) => {
return <div>{x.name}</div>;
})}
</h1>
</div>
);
}
}
export default App;
data
is an object, to access the teams array you need to use data.api.teams
which is the array of the teams. data
是一个对象,要访问团队数组,您需要使用data.api.teams
,它是团队的数组。 To access the first team in the array use data.api.teams[0]
要访问数组中的第一个团队,请使用
data.api.teams[0]
Change teamObj
to teams.将
teamObj
更改为团队。
state = {
teams: []
}
componentDidMount() {
fetch("https://api-football-v1.p.rapidapi.com/v2/teams/team/33", {
method: "GET",
headers: {
"x-rapidapi-host": "api-football-v1.p.rapidapi.com",
"x-rapidapi-key": "a35b8572b7mshe694b9e8ac66df4p158c4bjsn4518b48e8965"
}
})
.then(response => response.json())
.then(data => {
this.setState({teams : data.api.teams })
});
}
Render it like, team array will always contain one item, since you are passing the id of the team in your request.渲染它,团队数组将始终包含一个项目,因为您在请求中传递了团队的 id。 Access the team from the array with
this.state.teams[0].name
使用
this.state.teams[0].name
从数组访问团队
<h1>My fav team is: { this.state.teams.length && this.state.teams[0].name }</h1>
Try This尝试这个
import React from 'react';
import logo from './logo.svg';
import './App.css';
class App extends React.Component {
constructor() {
super();
this.state = {
teamObj:null
};
}
componentDidMount() {
fetch("https://api-football-v1.p.rapidapi.com/v2/teams/team/33",
{"method": "GET","headers":
{"x-rapidapi-host": "api-football-v1.p.rapidapi.com",
"x-rapidapi-key": "a35b8572b7mshe694b9e8ac66df4p158c4bjsn4518b48e8965"}
}
)
.then(response => response.json())
.then(data => {
console.log(data)
this.setState({teamObj : data.api.teams})
})
}
render() {
console.log(this.state.teamObj)
return (
<div>
<h1>My fav team is: {
this.state.teamObj &&
this.state.teamObj[0].name
}</h1>
</div>
);
}
}
export default App;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.