Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jsx is used without importing react

I am getting the

Jsx is used without importing react

Notification by WebStorm even tho I made react library global via:

My webpack.config.js

        plugins: [
            new webpack.ProvidePlugin({
                React: "react"
            })
        ]

My declarations.d.ts

import _React from "react";
declare global {
    const React: typeof _React;
}

It is no error and the code runs fine, but I do not like such an unnecessary notification. How do I tell WebStorm that I did import react globally?

like image 958
InsOp Avatar asked Dec 12 '19 08:12

InsOp


People also ask

Can I use JSX without importing React?

Starting from React 17, JSX transform use special jsx function internally. You don't need to import it.

Is it necessary to import React from React?

Based on that, we can go back to the original question of this article and understand the necessity to import React whenever we created a React component. Without it, the transformed JSX code would fail in the browser as React needs to be in scope to create components through its createElement function.

Why JSX is used in React?

JSX allows us to write HTML elements in JavaScript and place them in the DOM without any createElement() and/or appendChild() methods. JSX converts HTML tags into react elements. You are not required to use JSX, but JSX makes it easier to write React applications.

Can JSX be used without Babel?

React doesn't require babel but the library is built on the concept of using ES6 javascript syntax. React app however can be built with old ES5 syntax and JSX which would remove the need for Babel but you would lose the potential benefits of ES6.


2 Answers

Just turn the inspection off in Settings | Editor | Inspections | JavaScript and TypeScript | Imports and dependencies, Missing React import with JSX

like image 120
lena Avatar answered Oct 17 '22 13:10

lena


September 2022 Update

The setting is now under Editor > Inspections > JavaScript and TypeScript > Imports and dependencies > Missing 'React' namespace import in JSX code

To completely disable this inspection, select No highlighting as the Severity option.

The JetBrains Webstorm code inspection severity settings dropdown

like image 1
Malted_Wheaties Avatar answered Oct 17 '22 14:10

Malted_Wheaties