Arrow function without curly braces

前端 未结 7 1171
生来不讨喜
生来不讨喜 2020-11-22 10:52

I\'m new to both ES6 and React and I keep seeing arrow functions. Why is it that some arrow functions use curly braces after the fat arrow and some use parentheses? For exam

7条回答
  •  独厮守ぢ
    2020-11-22 11:17

    Parenthesis are used in an arrow function to return an object.

    () => ({ name: 'YourName' })  // This will return an object
    

    That is equivalent to

    () => {
       return { name : 'YourName' }
    }
    

提交回复
热议问题