在React中,为什么胖箭头函数不工作,除非它被包裹在括号中

In React, why is the fat arrow function not working unless it is wrapped in parenthesis

本文关键字:包裹 函数 为什么 React 工作      更新时间:2023-09-26

现在我知道在ES6中需要括号来返回对象字面量而不是执行代码块,下面的代码是否来自相同的React介绍页面在返回语句中做相同的事情?特别是.map(item =>()) -注意使用圆括号而不是花括号。谢谢你

类TodoList扩展React。组件{呈现(){回报(

    {this.props.items。Map (item => (){item.text} )}
)}}

这只是一个快捷方式。查看下面的示例:

const fn = () => ({name:'Jhon'})
const fn2 = () => {
    return {name:'Jhon'}
}
console.log(fn()); //Object {name: "Jhon"}
console.log(fn2()); //Object {name: "Jhon"}