Search code examples
javascriptreactjsreduxreact-redux

Line 4:13: 'React' is not defined no-undef


When i want to create an element without JSX it shows me this error 'React' is not defined no-undef. I am a beginner. I don't know what is the problem. Can anyone help

import logo from './logo.svg';
import './App.css';

const app = React.createElement("h1", null, "Without JSX");  // Showing an error

function App() {
  return (
    <div className="App">
      {app};
    </div>
  );
}

export default App;

These messages are shown in VS code Terminal.

Failed to compile.

./src/index.js
SyntaxError: E:\react_js_course\src\index.js: Unexpected token, expected "," (11:2)
Failed to compile.

src\App.js
  Line 4:13:  'React' is not defined  no-undef

Search for the keywords to learn more about each error.

Solution

  • A couple points just based on the code you provided:

    1. You are using JSX in your example: your App component returns JSX, so React should be in scope to do that.

    2. You are explicitly using the React method createElement so React should be available in this file.

    To fix both issues add import React from 'react'; at the top of this file.