3

I was practicing React-Routing, I want to separate constants in another file away from my App.js. Basically, these constants are content for my pages that I want to be editable in a separate environment. But after importing them to App.js, it gives error "'React' must be in scope when using JSX react/react-in-jsx-". Here is my App.js code:

import React from "react";
import "./App.css";
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
import {Home,About} from "./Constants.js";

class App extends React.Component {
  render() {
    return (
      <Router>
        <div>
          <li>
            <Link to="/">Home</Link>
          </li>
          <li>
            <Link to="/about">About</Link>
          </li>
          <Route exact path="/" component={Home} />
          <Route exact path="/about" component={About} />
        </div>
      </Router>
    );
  }
}

export default App;

and Constants in Constants.js:

const Home = (
  <div>
    <h2>Home</h2>
  </div>
);

const About = (
  <div>
    <h2>About</h2>
  </div>
);

export { Home, About };
Tholle
  • 108,070
  • 19
  • 198
  • 189
Bilal Kazmi
  • 180
  • 1
  • 14
  • 1
    Is the error not self explanatory? Which part of it do you not understand? – Felix Kling Jul 13 '18 at 21:34
  • Does this answer your question? ['React' must be in scope when using JSX react/react-in-jsx-scope?](https://stackoverflow.com/questions/42640636/react-must-be-in-scope-when-using-jsx-react-react-in-jsx-scope) – Henry Woody Oct 24 '22 at 21:28

1 Answers1

3

JSX gets compiled into React.createElement calls, so you need to import React if you are using JSX.

You should also write stateless functional components as functions, instead of as a React element directly.

import React from 'react';

const Home = () => (
  <div>
    <h2>Home</h2>
  </div>
);

const About = () => (
  <div>
    <h2>About</h2>
  </div>
);

export { Home, About };
Tholle
  • 108,070
  • 19
  • 198
  • 189