应为赋值,但在reactjs中看到了表达式
我是Reactjs的初学者。我正在尝试使用fetch从jasonplaceholder api获取注释并显示它们。 我收到以下错误:-应为赋值或函数调用,而不是看到表达式应为赋值,但在reactjs中看到了表达式,reactjs,Reactjs,我是Reactjs的初学者。我正在尝试使用fetch从jasonplaceholder api获取注释并显示它们。 我收到以下错误:-应为赋值或函数调用,而不是看到表达式 Code import React, { Component } from 'react' export default class Comments extends Component { constructor() { super() this.state = {
Code
import React, { Component } from 'react'
export default class Comments extends Component {
constructor() {
super()
this.state = {
comments: []
}
}
componentDidMount() {
fetch('https://jsonplaceholder.typicode.com/comments')
.then(response => response.json())
.then(comments => {
this.setState({ comments: comments })
});
}
render() {
console.log(this.state.comments, "comments")
return (
<div className="conatiner">
<p>All Comments :-</p>
{
this.state.comments.map((comment) => {
<ul key={comment.id}>
<li>{comment.email}</li>
<li>{comment.body}</li>
</ul>
})
}
</div>
)
}
}
code
从“React”导入React,{Component}
导出默认类注释扩展组件{
构造函数(){
超级()
此.state={
评论:[]
}
}
componentDidMount(){
取('https://jsonplaceholder.typicode.com/comments')
.then(response=>response.json())
。然后(评论=>{
this.setState({comments:comments})
});
}
render(){
console.log(this.state.comments,“comments”)
返回(
所有评论:-
{
this.state.comments.map((comment)=>{
- {comment.email}
- {comment.body}
})
}
)
}
}
这将起作用(返回映射中的元素)
this.state.comments.map((comment)=>(
- {comment.email}
- {comment.body}
))
reactjs中map的简单语法是
array.map((element,index)=>(
<h2>{element.name}</h2>
))
array.map((元素,索引)=>(
{element.name}
))
这将起作用(返回映射中的元素)
this.state.comments.map((comment)=>(
- {comment.email}
- {comment.body}
))
reactjs中map的简单语法是
array.map((element,index)=>(
<h2>{element.name}</h2>
))
array.map((元素,索引)=>(
{element.name}
))
添加return,因为它是一个回调函数
this.state.comments.map((comment) => {
return (
<ul key={comment.id}>
<li>{comment.email}</li>
<li>{comment.body}</li>
</ul>
);
})
this.state.comments.map((comment)=>{
返回(
- {comment.email}
- {comment.body}
);
})
更简单地说
this.state.comments.map((comment) => (
<ul key={comment.id}>
<li>{comment.email}</li>
<li>{comment.body}</li>
</ul>
))
this.state.comments.map((comment)=>(
- {comment.email}
- {comment.body}
))
添加return,因为它是一个回调函数
this.state.comments.map((comment) => {
return (
<ul key={comment.id}>
<li>{comment.email}</li>
<li>{comment.body}</li>
</ul>
);
})
this.state.comments.map((comment)=>{
返回(
- {comment.email}
- {comment.body}
);
})
更简单地说
this.state.comments.map((comment) => (
<ul key={comment.id}>
<li>{comment.email}</li>
<li>{comment.body}</li>
</ul>
))
this.state.comments.map((comment)=>(
- {comment.email}
- {comment.body}
))
typo:addareturn
insidemap.typo:addareturn
insidemap。