Error ReferenceError: React is not defined in Reactjs

Dung Do Tien May 23 2022 309

Hello developers, I'm a beginner in ReactJs. I want to create a component that helps me raw a list of data get from API. Like this:

 import { useEffect, useState } from 'react';

export default function MyComponent() {
  const [loading, setLoading] = useState(false);
  const [someData, setSomeData] = useState({});

  return (
    <div className={loading ? 'loading' : ''}>
      {someData}
      <a href="/loading/more">Loading more data!</a>
    </div>
  );
}

But when I was trying to run the code above, I get an exception threw ReferenceError: React is not defined.

 ReferenceError: React is not defined
    at MyComponent (App.tsx:7:5)
    at renderWithHooks (react-dom.development.js:16175:18)
    at mountIndeterminateComponent (react-dom.development.js:20913:13)
    at beginWork (react-dom.development.js:22416:16)
    at beginWork$1 (react-dom.development.js:27381:14)
    at performUnitOfWork (react-dom.development.js:26516:12)
    at workLoopSync (react-dom.development.js:26422:5)
    at renderRootSync (react-dom.development.js:26390:7)
    at recoverFromConcurrentError (react-dom.development.js:25806:20)
    at performConcurrentWorkOnRoot (react-dom.development.js:25706:22)

What is that mean? Do I have to import react into my component? 

I installed react version as below, but it's still not working for me:

 @types/react 18.0.8
@types/react-dom 18.0.2
react 18.1.0
react-dom 18.1.0

Thanks in advance!

Have 1 answer(s) found.
  • A

    Aleyna Bostan May 23 2022

    React app is required React module needs to import to your component. To import you can add the command below at the top of your component:

     import * as React from 'react';

    For example:

     import * as React from 'react';
    
    export default function App() {
      return (
        <div>
          <h1>Hello Reactjs!</h1>
        </div>
      );
    }

    I hope this answer is all you need.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close