【问题标题】:is this redundant? -- import React, { Component } from 'react'; [duplicate]这是多余的吗? -- 导入 React, { Component } from 'react'; [复制]
【发布时间】:2017-08-29 14:30:08
【问题描述】:

在我的Create React App 创建的文件App.js 的顶部是import React, { Component } from 'react';

React 不会已经导入所有内容...如果是,那么为什么需要 { Component }

【问题讨论】:

    标签: reactjs create-react-app


    【解决方案1】:

    单独导入Component 允许您使用Component 而不是React.Component。这很方便。

    【讨论】:

      【解决方案2】:

      这只是一个捷径,让你可以写

      class MyClass extends Component
      

      否则你必须在它前面加上前缀。

      【讨论】:

        【解决方案3】:

        如果你不使用 webpack 和 Tree Shaking 功能,那么它只是语法糖

        很多人都这么认为

        class MyComponent extends Component
        

        更漂亮
        class MyComponent extends React.Component
        

        但是如果你使用带有 tree shaking 的 webpack,那么 webpack 会在你的代码中执行死代码消除

        查看更多:

        https://webpack.js.org/guides/tree-shaking/

        【讨论】:

          猜你喜欢
          • 2018-11-12
          • 2017-11-14
          • 2018-06-13
          • 1970-01-01
          • 1970-01-01
          • 2018-12-19
          • 1970-01-01
          • 2018-09-08
          • 2019-10-06
          相关资源
          最近更新 更多