Course contentsShow
← React
Lesson 81 of 2,3963. JSX FundamentalsFree lesson

Why React Must Be In Scope (Classic Transform)

Understanding the legacy requirement to import React when using JSX with the classic transform.

Why React Must Be In Scope (Classic Transform)

You'll learn: Why older React code always imports React even when it seems unused.

The Mystery of the "Unused" Import

If you've seen legacy React code, you've probably noticed this pattern:

import React from 'react';

function Greeting() {
  return <h1>Hello!</h1>;
}

Notice React is imported but never explicitly referenced. Why?

JSX Becomes React.createElement

Remember that JSX compiles to function calls. With the classic transform (the original JSX compilation method), your JSX literally becomes React.createElement:

// You write:
<h1>Hello!</h1>

// Classic transform compiles to:
React.createElement('h1', null, 'Hello!');

The compiled code directly references the React object. If React isn't in scope, JavaScript throws a ReferenceError — it can't find React.

Think of It Like a Hidden Dependency

Analogy: Imagine a food processor that chops vegetables for you. The manual says "plug it in," but doesn't mention needing electricity. JSX is similar — it looks self-contained, but the compiled output has a hidden dependency on the React variable being available.

The classic transform assumes React.createElement exists in your scope, so you must import React at the top of every file using JSX, even if you never type "React" yourself.

Why "Classic"?

This is called "classic" because modern React (17+) introduced an automatic transform that eliminates this requirement. But millions of lines of existing React code still use the classic transform, which is why you'll see this import pattern everywhere.

Key Takeaway: With the classic JSX transform, import React from 'react' is mandatory because JSX compiles to React.createElement calls that directly reference the React object.