2

I have recently noticed that there is no import statement for react in App.js like:

import React from 'react'

In all the tutorials I have seen this line. Is it automatic now?

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
ag2byte
  • 191
  • 3
  • 11
  • 1
    Are you using the new JSX transform? Read this https://stackoverflow.com/a/64541142/12684693 and https://stackoverflow.com/a/64541142/3825777 – react_or_angluar Jan 10 '21 at 07:54

1 Answers1

12

It's a cool feature with React 17.

Read the official React docs about it here: https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html

here is a useful summary from the docs to see what's going on, in case you don't have time to read all of it:

What’s Different in the New Transform?

When you use JSX, the compiler transforms it into React function calls that the browser can understand. The old JSX transform turned JSX into React.createElement(...) calls.

For example, let’s say your source code looks like this:

import React from 'react';

function App() {
  return <h1>Hello World</h1>;
}

Under the hood, the old JSX transform turns it into regular JavaScript:

import React from 'react';

function App() {
  return React.createElement('h1', null, 'Hello world');
}

Note

Your source code doesn’t need to change in any way. We’re describing how the JSX transform turns your JSX source code into the JavaScript code a browser can understand.

However, this is not perfect:

  • Because JSX was compiled into React.createElement, React needed to be in scope if you used JSX.
  • There are some performance improvements and simplifications that React.createElement does not allow.

To solve these issues, React 17 introduces two new entry points to the React package that are intended to only be used by compilers like Babel and TypeScript. Instead of transforming JSX to React.createElement, the new JSX transform automatically imports special functions from those new entry points in the React package and calls them.

Let’s say that your source code looks like this:

function App() {
  return <h1>Hello World</h1>;
}

This is what the new JSX transform compiles it to:

// Inserted by a compiler (don't import it yourself!)
import {jsx as _jsx} from 'react/jsx-runtime';

function App() {
  return _jsx('h1', { children: 'Hello world' });
}

Note how our original code did not need to import React to use JSX anymore! (But we would still need to import React in order to use Hooks or other exports that React provides.)

This change is fully compatible with all of the existing JSX code, so you won’t have to change your components. If you’re curious, you can check out the technical RFC for more details about how the new transform works.

Note

The functions inside react/jsx-runtime and react/jsx-dev-runtime must only be used by the compiler transform. If you need to manually create elements in your code, you should keep using React.createElement. It will continue to work and is not going away.

You Can Read this blog about How to enable new JSX transform in React 17 too: https://dev.to/wojtekmaj/how-to-enable-automatic-runtime-in-react-17-with-babel-preset-react-52l

Taghi Khavari
  • 6,272
  • 3
  • 15
  • 32