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

前端 未结 4 810
生来不讨喜
生来不讨喜 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:22

    when you write myFunction = value => ({prop: value}) it return the object {prop: value}, in this case {} are object delimiter and not 'function delimiter'

    const updateChatMessage = text => ({
       type: types.someActionType,
       text
    })
    

    another eg :

    when you want to multiply by two each elem of an array you can write :

    array.map(elem => {return elem * 2})

    or

    array.map(elem => elem * 2) //same result

    and if you want an eg with () that wrap an object litteral :

    let array = [{val: 2},
                 {val: 4},
                 {val: 8},
                 {val: 16}];
                 
    let output = array.map( ({val}) => ({val: val*2}) );
    
    console.log(output);

提交回复
热议问题