1

Thoughout our TSX/React web application, we use two different import style for the react module:

import * as React from "react";

and

import React from "react"

I am not aware of any functional difference between the two. Both work just fine. Is there any reason to prefer one notion over the other?

Simon Warta
  • 10,850
  • 5
  • 40
  • 78
  • That totally depends. In the case of `React` both work fine as all the classes and methods are exported from the package along with combine `export default`. If a package doesn't export `default` then you will need to `import * as package`, so that you can have all the exported methods as an object. – Sonu Bamniya Nov 07 '19 at 09:10
  • @SonuBamniya Thanks. I'm aware of the difference in general but asking for the react package specifically. – Simon Warta Nov 07 '19 at 09:12
  • 1
    Alright! I think you should give a read to [this question](https://stackoverflow.com/questions/55285737/import-as-react-from-react-vs-import-react-from-react) – Sonu Bamniya Nov 07 '19 at 09:17
  • @SonuBamniya Amazing, my question has been answered there. My question might even be considered a duplicate. Could you convert that into an answer and I'll accept – Simon Warta Nov 07 '19 at 10:38
  • @JaredSmith, yes, thanks. This is the link Sonu posted as well. – Simon Warta Nov 07 '19 at 11:11
  • 1
    @SimonWarta I voted to close your question as a duplicate of the other which causes the system automatically add the comment. – Jared Smith Nov 07 '19 at 11:12

2 Answers2

1

You want to do import React from "react" so you editor can auto change it to import React, { useState } from "react".

Simon Warta
  • 10,850
  • 5
  • 40
  • 78
Xizam
  • 699
  • 7
  • 21
0

As per my knowledge both are same.

import everything from "react" and then we can use it as React in our application

  import * as React from "react"; 

same as

 import React from "react";

So as per me you need to use import React from "react";.Because you can directly access React from react npm.

Prakash Karena
  • 2,525
  • 1
  • 7
  • 16