React.js-“无法读取未定义的属性'map'”

时间:2020-06-13 18:19:30

标签: javascript arrays reactjs material-ui array-map

我尝试了通过搜索找到的方法,但是没有用?为什么仍显示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```



1 个答案:

答案 0 :(得分:2)

response.json()返回promise。您应该更改行

  const data = response.json();

  const data = await response.json();

顺便说一句,在await中使用const url = await 'https://api.covid19india.org/data.json';是多余的