fetch()在JS(React)中检索数据,丢弃错误,没有明显的原因
我认为与文本编辑器(Atom)中的Babel或JavaScript语言软件包有关,因为我实际上是在沿着教程进行编码,并且我才刚刚开始,并且在运行“ NPM启动或Yarn start”后我已经遇到了错误。我的代码与教程中的代码完全相同,但是在我的情况下,我提示了以下错误:
这是我的代码:
import './App.css'
import React, {Component} from 'react'
class App extends Component {
constructor(){
super()
this.state= {
monsters: []
};
}
componentDidMount(){
fetch('https://jsonplaceholder.typicode.com/users')
.then(response => response.json())
.then(data =>
this.setState(
()=>{
monsters: data
}
)
);
}
render(){
return <div className='App'>
<h1>{this.state.monsters}</h1>
<ul>{this.state.monsters.map((monster)=>{
return <li>{monster.name}</li>;
})}</ul></div>;
}
}
export default App;
我希望有人可以提示我应该做什么或尝试,为了解决这个问题...
I think is related to babel or javascript-language package in the text editor (Atom), because I am actually coding along a tutorial and I am just starting and I already get error after running "npm start or yarn start". I have exactly the same code as in the tutorial but in my case it prompts the following errors:
This is my code:
import './App.css'
import React, {Component} from 'react'
class App extends Component {
constructor(){
super()
this.state= {
monsters: []
};
}
componentDidMount(){
fetch('https://jsonplaceholder.typicode.com/users')
.then(response => response.json())
.then(data =>
this.setState(
()=>{
monsters: data
}
)
);
}
render(){
return <div className='App'>
<h1>{this.state.monsters}</h1>
<ul>{this.state.monsters.map((monster)=>{
return <li>{monster.name}</li>;
})}</ul></div>;
}
}
export default App;
I hope someone can give me a hint on what should I do or try, in order to fix this...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的卷曲括号使其成为一个函数,并且期望
返回
值。您的目的是返回对象。为此,添加括号。那应该解决这个问题。
The curly braces here make this a function, and it's expecting a
return
value. Your intention is to return an object. To do that, add parentheses.That should do the trick.