我尝试创建反应组件,将feed URL作为props接受,然后显示数据。 在代码中: LeageMatch的1-LeagueInfo子组件。 2-LeageMatch父组件它是主要组件,我将feed传递给LeaguInfo(它的孩子)作为index.js中的道具
`
/////LeagueInfo-Child compnent////
import React, { Component } from 'react';
class LeagueInfo extends Component{
constructor(props){
super(props);
this.state = {
error: null,
isLoading : false,
league : '',
feed : this.props.feed
}
this.apiUrl = process.env.REACT_APP_API_URL;
console.log(this.apiUrl);
this.API =this.apiUrl+this.state.feed+".json"
}
componentDidMount(){
// const API =this.apiUrl+this.state.feed+".json" ???
this.setState({ isLoading: true });
fetch(this.API,{
method: 'get',
dataType: 'json',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
})
.then((response) => response.json())
.then(data=>{
let league = <p> <img src={"feedurl"+(data.competitionId)+".png"} /> {data.competitionName}</p>;
this.setState({league: league});
})
.catch(error => this.setState({ error, isLoading: false }));
}
render(){
return(
<h2>
{this.state.league}
</h2>
);
}
}
export default LeagueInfo;
/////LeageMatch-Parent compnent////
import React, { Component } from 'react';
import LeagueInfo from './LeagueInfo'
class LeagueMatch extends Component {
constructor(props){
super(props);
this.state = {};
}
render() {
return (
<LeagueInfo feed={this.props.leagueInfofeed} />
...);
}
}
}
export default LeagueMatch;
//index.js
const LeagueMatchWrapper = document.getElementById('leaguematch-wrapper');
ReactDOM.render(<LeagueMatch leagueInfofeed="league"/>, LeagueMatchWrapper);
` 在代码中 我有新的反应,所以有一些问题:
feed :
this.props.feed
。const API =this.apiUrl+this.state.feed+".json"
当组件首先呈现时它显示正确的数据然后它隐藏它,我能知道为什么吗?