答案 0 :(得分:1)
componentDidMount
生命周期功能是您需要向API请求的地方。初始渲染后调用componentDidMount
class SearchForm extends React.Component {
constructor(props) {
super(props)
this.state = {
position: '',
area: '',
date: '',
experience: {
type: Array,
default: function () { return [] }
}
}
this.handlePositionChange = this.handlePositionChange.bind(this);
this.handlePositionKeyUp = this.handlePositionKeyUp.bind(this);
this.handleAreaChange = this.handleAreaChange.bind(this);
}
componentDidMount() {
//API request here
APICall().then(res => {
this.setState({data: res});
})
}
...
render() {
//conditional rendering here
if(!this.state.data) {
return 'Loading...'
}
return (
<form className='form search-form' onSubmit={this.handleSubmit}>
<div className="form-group col-md-2">
<label htmlFor="experience">Experience</label>
<select className="form-control" name="experience" id="experience" onChange={this.handleChange} value={this.state.experience}>
<option key={this.props.experience.id} value={this.props.experience.name}>
{this.props.experience.name}
</option>
</select>
</div>
</form>
)
}
}
export { SearchForm }