嗨,我正在从 api 获取数据,我想获取数据并将其渲染到 dom 但我是错误“未捕获的类型错误:无法读取 Topicselect.render 处未定义的属性‘地图’”
这基本上就是我正在做的事情,尽管我已经抽象出与问题不直接相关的任何内容,例如实际主题名称、导入等:
class Topics extends Component{
constructor(props){
super(props);
this.state = {
topics: []
}
}
componentWillMount(){
fetch('/api').then((res)=>r.json().then((data)=>{
// push topics into this.state.topics somehow
})
console.log(this.state.topics) //returns ['topic1','topic2','topic3'];
}
render(){
const list = this.state.topics.map((topic)=>{
return(<li>{topic}</li>);
})
return(
<ul>
{list}
</ul>
)
}
}
谁能告诉我如何解决这个问题?我在这里看到一个答案说使用 componentDidMount 而不是 componentWillMount 但这对我不起作用