【问题标题】:React js - Difference between "const example = () => {}" and "function example () {}" [duplicate]React js - “const example = () => {}”和“function example () {}”之间的区别 [重复]
【发布时间】:2021-09-17 15:44:56
【问题描述】:

我已经构建了这段代码:


const example = () => {

  console.log("test");

}
function example() {
 
  console.log("test");

}

既然它们的输出和用途是一样的,那么两者有什么明显的区别吗?

【问题讨论】:

    标签: reactjs function constants


    【解决方案1】:

    我唯一能找到的是关于提升,如果你在声明它之前使用了一些组件,你应该使用函数,这样你的 linter 就不会抛出错误。

    这样

     const App = () => (
        <>
          <MyComponent />
          <AlsoMyComponent />
        </>
    )
    // I like to keep my components at the bottom
    
    function MyComponent() {}
    
    function AlsoMyComponent() {}
    

    看看这个:https://dev.to/ugglr/react-functional-components-const-vs-function-2kj9

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-27
      • 2020-02-03
      • 2012-02-13
      • 1970-01-01
      • 2012-03-12
      • 2018-06-20
      • 2011-05-17
      相关资源
      最近更新 更多