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.
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).
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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.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).