所以我试图用 React 弄脏我的手。我非常决心使用 ReactJS编写维基百科查看器项目。然而,在使用 React 组件从维基百科的 API 获取数据后,我昨天遇到了麻烦。我认为这与异步调用有关。
我定义了一个接受查询并将其注入 API url 的常量:
javascript
const apiUrl = query =>
`https://crossorigin.me/https://en.wikipedia.org/w/api.php?action=opensearch&format=json&search=${query}`
然后我定义了一个在 ComponentDidMount 状态下异步获取 API 的类:
class Wiki extends React.Component {
constructor(props){
super(props);
console.log('Inside Constructor')
}
componentWillMount(){
this.setState = {
wikiData: undefined
}
console.log('Before fetch')
fetch(apiUrl(this.props.query))
.then(console.log('fetch successful'))
.then(response => {
if(!response.ok){
throw Error("Response not ok")
}
return response
})
.then(data => data.json())
.then(data => {
this.setState ={
wikiData: data
}
console.log(data)
})
}
render(){
if(!this.state.wikiData == null) return <p>No answer</p>
else{
return (
<div>
<h1>Something happened</h1>
<h2>{this.state.wikiData[0]}</h2>
</div>
)
}
}
}
当我在应用程序组件中调用它时,我收到一个 TypeError sying "Cannot read property 'wikiData' of null",之后数据从调用返回并且不再为 null,但这会导致渲染中断。 控制台消息 我确实尝试使用 componentDidMount 代替,但这也不起作用。我不知道该怎么做,我想了解这个过程。这是我的代码笔:https ://codepen.io/Banhawy/pen/eEmQBW ? editors = 1010
我想知道如何仅在对 API 的调用返回数据后才呈现组件,以便我可以在呈现之前访问和处理它。