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 };
2

1 Answer 1

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 };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.