25

I was getting this error while adding a View component in .tsx file.

udai
  • 3,723
  • 4
  • 16
  • 20
  • Does this answer your question? ['React' refers to a UMD global, but the current file is a module](https://stackoverflow.com/questions/64656055/react-refers-to-a-umd-global-but-the-current-file-is-a-module) – Liam Jun 21 '22 at 19:52

5 Answers5

48

I raised this question and answered myself as I thought it will help others who are facing similar issue.

Please import React in your code import statements:

import * as React from 'react';

Reference:

https://github.com/Microsoft/TypeScript/issues/14118

Jan Klimo
  • 4,643
  • 2
  • 36
  • 42
udai
  • 3,723
  • 4
  • 16
  • 20
11

One don't need to import everything using *

It's just react which is missing and needs to be imported so that JSX can be understood.

So just do following

import React from 'react';
Stéphane Bruckert
  • 21,706
  • 14
  • 92
  • 130
TARJU
  • 1,785
  • 1
  • 14
  • 16
  • Except it's not recommended: https://twitter.com/dan_abramov/status/1308739731551858689?s=20&t=2sXKpECWNDoZh5rKDMZJWA – dalvallana Nov 08 '22 at 08:58
4

With the introduction of a new JSX transform in React 17, it is no longer necessary to import React in every file that uses JSX. If you are using TypeScript, edit the jsx compiler option in your tsconfig to use "react-jsx".

{
    "compilerOptions": {
        "jsx": "react-jsx"
    }
}

The old default transformation ("jsx": "react"), compiled JSX into calls to React.createElement, so React had to be in scope for it to work. The react-jsx transform, however, uses the _jsx function and automatically inserts the import for it. After this change, unused imports of React can be safely removed.

See also: https://legacy.reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
2

Try importing in this way

import React, {Component} from 'react';
Nilesh Tiwari
  • 194
  • 2
  • 7
-1

Use any of the following to remove the error:

declare var React: any;
var React;
import React from "react";
import * as React from "react";