[英]React.js - “Cannot read property 'map' of undefined”
I have tried the methods that I found by searching but none of that work?我已经尝试了通过搜索找到的方法,但这些方法都不起作用? Why it's still showing that map
is undefined?为什么它仍然显示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```
response.json()
return a promise
. response.json()
返回一个promise
。 you should change the line你应该换行
const data = response.json();
to至
const data = await response.json();
And, by the way, using await
in const url = await 'https://api.covid19india.org/data.json';
顺便说一句,在const url = await 'https://api.covid19india.org/data.json';
中使用await
; is redundant是多余的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.