Member-only story

Why ‘React’ should be imported when not used?

Vinodh Thangavel
2 min readJun 22, 2023

--

Photo by Ali Hajian on Unsplash

Whenever we consume a variable, we either need to declare in the same file or we need to import from a file that has been declared and exported

Declare in that file or import from other files where it is declared

The above examples have the logic as we are using tax in our file so we expect that to be declared, but why React we are expected to import React when we are not using it?

It is because JSX (Javascript extension to have HTML in JS file) will not be understood by browser, so we need to convert in to ones(HTML, CSS, Javascript) which browser can understand.

So even though we are not directly consuming React, when converted to plain JS while bundling, it will use React.createElement method to create actual DOM

So we need to import React whenever HTML syntax is used in JS file.

If you don’t use HTML in JS, we don’t need to import React.

--

--

Vinodh Thangavel
Vinodh Thangavel

Written by Vinodh Thangavel

Passionate lifelong learner, coding enthusiast, and dedicated mentor. My journey in tech is driven by curiosity, creativity, and a love for sharing knowledge.

No responses yet