如果有人解释了为什么在updatePosts中我们应该返回一个对象,而在对象内部我们应该再次返回它,我将不胜感激。为什么我们可以只做代码和平=>
const UpdatedPosts = posts.map(data =>{...数据,作者:“狮子座”})
class Blog extends Component {
state = {
post : []
}
componentDidMount(){
axios.get('https://jsonplaceholder.typicode.com/posts')
.then(response => {
const posts = response.data.slice(0,3);
const updatedPosts = posts.map(data =>{
return {
...data,
author : 'Leo'
}
})
this.setState({post : updatedPosts})
console.log(response)
})
这段代码const updatedPosts = posts.map(data => { ...data, author : 'Leo' } )
实际上是可行的,但是必须在对象周围加上括号,这可以为您提供此功能
const updatedPosts = posts.map(data => ({ ...data, author : 'Leo' }))
您可以在本文档的[[返回对象文字]部分中找到说明https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions