Import React vs React, { Component }

前端 未结 3 1484
伪装坚强ぢ
伪装坚强ぢ 2020-12-08 22:27

Import React vs Import React, { Component }

Which one is better and why?

Or does it make no difference other than writing less cod

3条回答
  •  失恋的感觉
    2020-12-08 23:10

    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 Component, React. is not needed. All React modules are imported, but the modules inside the curly brackets are imported in such a way that the React namespace prefix is not needed when accessing.

提交回复
热议问题