Tho*_*lin 11 javascript jsx reactjs
我正在尝试使用版本为 4.0.0 的 create-react-app 运行我的第一个 React JSX 文件,并且它可以工作!但是,我的 JSX 中没有包含 React 的 import 语句,它也可以工作,但是很奇怪。该项目是通过create-react-app命令创建的,版本为 4.0.0。
App.js 文件:
/* 没有导入 React 但也可以工作!*/
import { Component } from "react";
class App extends Component {
render() {
return <div>456</div>;
}
}
export default App;Run Code Online (Sandbox Code Playgroud)
nor*_*ial 17
Starting from React 17 it's not necessary to import React to use JSX. See from the blog post about that in the section called Removing Unused React Imports:
因为新的 JSX 转换会自动导入必要的 react/jsx-runtime 函数,所以当你使用 JSX 时,React 将不再需要在作用域内。这可能会导致您的代码中出现未使用的 React 导入。
什么是 JSX 转换?
浏览器无法立即理解 JSX,因此大多数 React 用户依赖于 Babel 或 TypeScript 之类的编译器将 JSX 代码转换为常规 JavaScript。
建议阅读Introducing the New JSX Transform。您还可以看到Using React 17 和新的 JSX 转换与 Create React App 4.0 Alpha。