This is my code
My state
this.state = {
loading: true,
weather: [],
cityName: [],
selectedOption: '',
}
My call api
const getAPIcityName = Axios.get('http://dataservice.accuweather.com/locations/v1/topcities/100?').then((res) => {
console.log('res' + res);
this.setState({ cityName: res.AdministrativeArea.LocalizedName });
}, (error) => {
console.log('error', error)
});
}
And here i wont to do the Select the name of the city
handleChange(selectedOption) {
this.setState({ selectedOption });
console.log(selectedOption);
}
render() {
let options = this.state.cityName.map((cityName) => {
return cityName.AdministrativeArea.LocalizedName;
})
return (
<div class="container">
<h1 for="Search">Search</h1>
<form>
<Select
name="form-field-name"
value={this.state.value}
onChange={this.handleChange}
options={options}
/>
And here it works
{/* <select class="custom-select custom-select-lg mb-3">
{this.state.cityName.map(cityName => <option key={cityName.Key}>{cityName.AdministrativeArea.LocalizedName}</option>)}
</select> */}
And this is the Error
Unhandled Rejection (TypeError): Cannot read property 'LocalizedName' of undefined
cityName.AdministrativeAreaisundefined, that's why you are getting that error. you need to initialize it in your constructor or await your API call.