tonwayer
tonwayer

Reputation: 532

Do I need to import react in every file?

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?

Upvotes: 37

Views: 29570

Answers (3)

Safin Sabu
Safin Sabu

Reputation: 1

if you don't use jsx code in your file, react import is not necessary

Upvotes: -1

Ahmed Tolan
Ahmed Tolan

Reputation: 111

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

Upvotes: 10

Tarek Hammami
Tarek Hammami

Reputation: 1168

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.

Upvotes: 71

Related Questions