【问题标题】:React.js - "Cannot read property 'map' of undefined"React.js - “无法读取未定义的属性‘地图’”
【发布时间】:2020-06-13 18:19:30
【问题描述】:

我已经尝试了通过搜索找到的方法,但都不起作用?为什么它仍然显示 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```



【问题讨论】:

  • 数据对象结构是什么?您可以通过登录控制台分享吗?
  • 我得到了答案,实际上,它返回了印度各州的列表

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


【解决方案1】:

response.json() 返回一个promise。你应该换行

  const data = response.json();

  const data = await response.json();

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

【讨论】:

    猜你喜欢
    • 2017-11-10
    • 1970-01-01
    • 2019-12-20
    • 1970-01-01
    • 1970-01-01
    • 2021-03-29
    • 1970-01-01
    • 2019-12-30
    • 2016-11-06
    相关资源
    最近更新 更多