函数声明
function MyComponent(props, context) {}函数表达式
const MyComponent = function(props, context){}箭头函数
const MyComponent = (props, context) => {}发布于 2015-12-03 02:55:52
函数声明,如果您想要提升,并且更喜欢可读性而不是性能。
函数表达式,如果你想命名你的函数,这样你就可以更容易地从调试堆栈跟踪中识别它(例如,从Chrome开发工具中)。
如果您不关心在堆栈跟踪中是否有匿名函数,并且希望避免绑定this,则可以使用箭头函数。
发布于 2016-05-14 19:07:14
我的无状态组件看起来像这样,在我看来非常干净,看起来很像HTML。此外,如果您不使用括号,es6箭头函数将假定返回表达式,因此您可以省略这些表达式:
const Table = props =>
<table className="table table-striped table-hover">
<thead>
<tr >
<th>Name</th>
<th>Contact</th>
<th>Child Summary</th>
</tr>
</thead>
<tbody>
{props.items.map(item => [
<TableRow itemId={item.id} />,
item.isSelected && <TableRowDetails itemId={item.id} />
])}
</tbody>
</table>https://stackoverflow.com/questions/34047523
复制相似问题