React style guide

This is taken from the Airbnb react style guide

// bad
class Listing extends React.Component {
  render() {
    return <div>{this.props.hello}</div>;
  }
}
// bad (relying on function name inference is discouraged)
const Listing = ({ hello }) => (
  <div>{hello}</div>
);
// good
function Listing({ hello }) {
  return <div>{hello}</div>;
}

之所以不推荐是因为Inferred function names

先看看MDN的解释Function.name

Inferred function names
Variables and methods can infer the name of an anonymous function from its syntactic position (new in ECMAScript 2015).

即在ES6中,浏览器会去推断匿名函数的name属性

而且在babel中,在对arrow function转译时也会加上函数名

image.png

转译称ES5以后


image.png

简单来说,就是你的代码和你预期的行为是一样的是比较好的实践,尽量避免浏览器和预处理器的隐式的行为

reference

Why does the Airbnb style guide say that relying on function name inference is discouraged?

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容