What do parenthesis surrounding brackets in the return statement of an ES6 arrow function do?

前端 未结 4 809
生来不讨喜
生来不讨喜 2021-01-25 07:29

For example in redux actions, I\'ve seen in someone\'s code:

export const updateMessage = text => {
   return (dispatch) => {
     dispatch(updateChatMess         


        
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-25 08:07

    As per the arrow function docs,

    // Parenthesize the body of a function to return an object literal expression:

    params => ({foo: bar})

    That means if you want to return an object implicitly you have to wrap it in parentheses.

    Without this, code inside braces will be considered as function body and not an object (as you'd want)

    Following are equivalent:

    params => { return {foo: bar}} // Explicitly return an object (from function body)
    params => ({foo: bar}) // Implicitly return an object by wrapping it with parentheses
    

提交回复
热议问题