Javascript ES6 ()=>() 和 ()=>{} 的区别

Javascript ES6 Difference between ()=>() and ()=>{}

我在学习 React 时看到过类似的代码

const LinkCell = ({rowIndex, data, col, ...props}) => (
  <Cell {...props}>
    <a href="#">{data.getObjectAt(rowIndex)[col]}</a>
  </Cell>
);

此外,到目前为止我认为在 ES6 函数中 shorthand 是

let sum = (a, b)=>{
   return a + b;
}

第一个和第二个有什么不同?

使用 ()=> () 语法想象一下是否存在隐含的 return 语句,例如() => {return ()}

() => ()() => { doSomething() OR return; } 的单行 shorthand。

无论如何,如果您需要更多操作并且需要多行语句,您应该使用 () => {} 语法,否则您可以使用 shorthand 语法 () => ()

以下也视为一行语句。但是要使用with() => ()语法,需要重写没有return语句,

// The below one line statement can be rewritten as below
if (true ) return something;

// rewritten of above one
() => ( a > b ? a : b)

// one line statement
if (true ) invoke();  // This will go like, () => (a ? a.invoke() : b.invoke())

// one line statement
for(var i in results) doSomething();

//And your jsx statement which can be tread as one liner
<Cell {...props}>
    <a href="#">{data.getObjectAt(rowIndex)[col]}</a>
  </Cell>