2015-12-02 52 views
0

函数声明哪一个是您在React.js中声明无状态功能组件的首选方式?为什么?

function MyComponent(props, context) {} 

函数表达式

const MyComponent = function(props, context){} 

Arrow功能,如果你想提升,喜欢的可读性超性能

const MyComponent = (props, context) => {} 
+0

这是毫无意义因为它们都只是JavaScript函数,这已经回答了前[](HTTP ://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname) – enjoylife

回答

0

函数声明。

函数表达式,如果你想命名你的函数,所以你可以更容易地从调试堆栈跟踪(例如从Chrome开发工具)识别它。

箭头函数如果你想如果你不关心在你的堆栈跟踪中有一个匿名函数,并且想要避免绑定this

0

我的无状态组件看起来像这样,对我来说看起来很干净,几乎看起来就像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> 
相关问题