前端踩坑(二)--------------------------Import React vs React, { Component }

转载https://stackoverflow.com/questions/39361282/import-react-vs-react-component

Import React vs Import React, { Component }

Which one is better and why?

Or does it make no difference other than writing less code later on?

Does writing { Component } mean it only imports the Component object?

 


导入React与导入React,{Component}

哪一个更好,为什么?

或者除了稍后编写更少的代码之外它没有任何区别?

写{Component}是否意味着只导入Component对象?

 

 

import React, { Component } lets you do class Menu extends Component instead of class Menu extends React.Component. It's less typing and duplication of the React namespace, which is generally a desired modern coding convention.

Additionally, tools like Webpack 2 and Rollup do "tree shaking," meaning any unused exports are not bundled into your final code. With import React/React.Component you are guaranteeing all of React's source code will be bundled. With import { Component }, some tools will only bundle the code needed to use the Component class, excluding the rest of React.

The above paragraph is irrelevant in this specific case, because you always need to have React in the current namespace to write JSX, but only importing the exact modules you need in other cases may lead to smaller bundled code in the end.

Beyond that it's entirely personal preference.

16.7k66096

  • So I'm still importing PropTypes and everything else that comes with React? It's not like I'm just importing Component? – epiqueras Sep 7 '16 at 4:41 

  • @epiqueras You are importing everything, but Component is imported in a way that you don't need to reference it as React.Component, but instead, just Component – Li357 Sep 7 '16 at 4:43

  • Just fyi, even if you were just doing { Component } from 'react', your bundler will still bundle the ENTIRE dependency so if you were worried about size, this isn't the way to reduce it. – ZekeDroid Sep 7 '16 at 4:44

  • @AndrewL. Is there a name for this type of import? – epiqueras Sep 7 '16 at 4:44

  • It's just shorthand to import anything on the React namespace as a variable you can use. You can also import React, { PropTypes, Component } from 'react' if you want. If this didn't answer your question please edit the question to be more specific, StackOverflow is not good for back and forth chat type answers. – Andy Ray Sep 7 '16 at 4:45 

 

What these are are named imports or namespace imports. What they do is basically copy over the module's contents into the namespace allowing:

import React, { Component } from 'react';

class SomeComponent extends Component { ... }

Normally, we'd extend React.Component, but since the Component module is imported into the namespace, we can just reference it with ComponentReact. is not needed. All React modules are imported, but the modules inside the curly brackets are imported in such a way that the Reactnamespace prefix is not needed when accessing.

it's entirely personal preference.

Just a note…

import React, { Component } lets you do class Menu extends Component instead of class Menu extends React.Component. It's less typing…

If you want to do less typing, then don't import Component in addition to React.

import React, { Component } from 'react';
class Menu from Component {

is more typing than:

import React form 'react';
class Menu from React.Component {

even if you consider auto-completion. ;)

看完发现这两种区别是为了减少码农们的输入量研究的,  导入所需要的内容,现代编码约束:)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小李科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值