我正在尝试从父组件character=this.state.character
调用prop,以此作为从JSON文件中获取数据的条件。
import React, {Component} from 'react'
import Axios from 'axios'
export default class CharacterProfile extends Component {
constructor(props) {
super(props);
this.state= {
profile:[]
}
}
componentDidMount() {
this.serverRequest =
Axios
.get("https://api.myjson.com/bins/yp4sh.json")
.then(result => {
if (this.props.character === 'Roddick'){
this.setState({
profile: result.data[0]
});
} else {
this.setState({
profile: result.data[1]
});
}
})
}
render () {
let whichClass = this.props.characterMenu!=='profile' ?
'hideElement' : this.props.characterSelected ?
'blueBackground' : 'hideElement';
let character = this.props.character;
let arraya = this.state.profile
return(
<div className={whichClass}>
<h1>{character} Profile</h1>
{this.state.profile['Character']}
</div>
)
}
}
但是,当我尝试执行此操作时,代码似乎完全忽略了if (this.props.character === 'Roddick')
。有没有办法在此函数内使用道具,以便我可以有条件地获取数据?