DEV Community

Discussion on: You don’t always need to import React

Collapse
 
regularfry profile image
Alex Young

So here's a question I honestly don't know the answer to: why isn't import React from "react"; inserted at the transpilation step from jsx to js? Why do we need to do this manually? For the case where all you're doing in the file is using jsx tags, the import is confusing and should be unnecessary.

Collapse
 
dance2die profile image
Sung M. Kim • Edited

Thanks Alex, that's a great question I haven't considered.

Inserting the import statement would require a babel plugin (babel-plugin-react-require or babel-plugin-auto-import, etc), which could relieve us from having to manually insert it where JSX is used.

I honestly didn't know "why" babel doesn't import it automatically so dug around a bit.

It's to prevent import name resolution errors according to following closed issues.

The gist is that, importing react automatically can clash with other imports already declared as "React" or when "React" is declared globally in your application (using "script" tag).