我尝试了通过搜索找到的方法,但是没有用?为什么仍显示map
未定义?
import TextField from '@material-ui/core/TextField';
import Autocomplete from '@material-ui/lab/Autocomplete';
class ComboBox extends React.Component {
state ={
option : []
}
async componentDidMount(){
const url = await 'https://api.covid19india.org/data.json';
const response =await fetch(url);
const data = response.json();
const pop = data.statewise.map(st=>st.state);
this.setState({option : pop})
}
render(){
return (
<Autocomplete
id="combo-box-demo"
options={(this.state.option)}
style={{ width: 300 }}
renderInput={(params) => <TextField {...params} label="Combo box" variant="outlined" />}
/>
);}
}
export default ComboBox```
答案 0 :(得分:2)
response.json()
返回promise
。您应该更改行
const data = response.json();
到
const data = await response.json();
顺便说一句,在await
中使用const url = await 'https://api.covid19india.org/data.json';
是多余的