Do I need to import react in every file? - reactjs

I've found out that components without import React from 'react'; lines works well.
I've added import React from 'react'; to the first line of .jsx files conventionally. And I saw many open source with this line.
Then why do we add this line unnecessarily?

You no longer need to import React from "react". Starting from the release 17 of React, JSX is automatically transformed without using React.createElement.However, other exports like hooks must be imported.

if you use a builder like esbuild, unfortunately you will need to import react in every file because it would be an error: Uncaught ReferenceError: React is not defined
but if you use the default builder you don't have to import react in each file

Related

Error : 'React' must be in scope when using JSX react/react-in-jsx-scope. (tried import react but not working)

enter image description here
please check the jpg & help me out if you need the code then comment down below
In older react versions import React from 'react had to present in the file when jsx was used.
Either add
import React from 'react
to the beginning of the file or update react version to 17 or 18

Why can't I import React as "react"?

Problem described here too, but the response was not elaborative React can't be found
import React from 'react' <- I know this statement is correct
Since "React" is a default export and not a named export, shouldn't this statement work too:
import react from 'react'
I know React.createElement() will be called in future, but why isn't react.createElement() correct? After all, the word "React" is just a name to refer to 'react' module.
In the old versions of react-scripts which uses webpack as a bundler, you need to define a React object in your code where you use JSX because when the bundler is handling your code uses the defined React object to call for the nessecerary methods like React.createElement and everywhere else that react is needed. That is why if you remove the React import or write the name in any other fashion you will face an error

Does import React from "react" store the entire react library in the React object or is the React object 1 of many objects in the react library?

I have been studying React and I am trying to understand conceptually what the import statement that I see at the top of every React.js file import React from "react" is actually doing. I have been reading through the React docs https://reactjs.org/docs/react-api.html but not sure if I am understanding the concept correctly.
Does the import statement import React from "react" mean that I am importing the entire react library into the .js file and storing the entire react library inside a new object I am naming "React"?
Or is the "React" object an object that is already defined inside the react library and I am using the import statement to pull the "React" object out of the react library to use some of its methods? In other words the react library contains other objects as well as the React object from my import statement and my imports could be something like this
import React from "react"
import someOtherObject from "react"
As you import a module from your JavaScript file, the module will be cached, for example by Node.js or a browser. Importing the same module from multiple JavaScript files won't create a new object each time.
Although you can use React APIs like React.useState, destructured named imports (ex. import { useState } from "react") is the preferred style going into the future according to this post from React Blog.

what am I doing wrong with this import in my react component?

What am I doing wrong with this import in my react component? I'm trying to use the following npm package in my React app:
https://www.npmjs.com/package/chrome-headless-render-pdf
I'm assuming that this package is useable from a React app. Is there any reason why it would not be? If not then how should I evaluate whether or not an npm package is useable in a React app? The npmjs page shows the package being used like this:
const RenderPDF = require('chrome-headless-render-pdf');
RenderPDF.generateSinglePdf('http://google.com', 'outputPdf.pdf');
I was thinking that I should be able to simply import the package in my React component like this:
import * from 'chrome-headless-render-pdf';
However, intellisense is reporting this import as invalid. How can I properly import this package into my component?
In their documentation, They also mentioned it as:
you can also use it from typescript or es6
import RenderPDF from 'chrome-headless-render-pdf';
RenderPDF.generateSinglePdf('http://google.com', 'outputPdf.pdf');
This line is not a valid import statement according to the ecmascript spec.
import * from 'chrome-headless-render-pdf';
When you use the * import syntax, you must assign a name. For example:
import * as chromeHeadless from 'chrome-headless-render-pdf';
This will make all named exports from the module available from your chosen namespace. You might use this with modules that does not have a default export. Typically the documentation for the module will explain which style of import syntax you can use.
MDN provides a reference of the different valid import statements available

Why are some ES6 imports globally available, while others are not?

I use standard ES6 imports in a react project that is built with Webpack.
For example, at the top of my files will be statements like this:
import React, { Component } from 'react';
import _ from 'lodash';
I found that if multiple files use React, I need to import it in each file.
But I only need to import lodash in one file, and it is available everywhere.
Why the difference?
Looks like this is a lodash specific issue - when imported using Webpack, it can leak into the global window.
https://github.com/lodash/lodash/issues/1798

Resources