无需导入React就可以使用JSX [英] JSX can be used without importing React

查看:824
本文介绍了无需导入React就可以使用JSX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用版本4.0.0的create-react-app运行我的第一个React JSX文件,它可以工作!但是,我的JSX中没有React的import语句,它也可以工作,但是很奇怪.该项目是通过 create-react-app 命令创建的,版本为4.0.0.

I'm trying to run my first React JSX file with create-react-app with version 4.0.0 and it works! However, I don't have the import statements of React included in my JSX and it works too but it is very weird. The project is created by create-react-app command with version 4.0.0.

App.js文件:

/*不导入React,但也可以! */

/* without import React but works too! */

 import { Component } from "react";
class App extends Component {
  render() {
    return <div>456</div>;
  }
}
export default App;

推荐答案

This is new with the JSX transform introduced with React 17 :

与React 17版本一起,我们希望做出一些 JSX转换的改进,但我们不想破坏 现有设置.这就是为什么我们与Babel合作,提供新的, JSX转换的改版 升级.

Together with the React 17 release, we’ve wanted to make a few improvements to the JSX transform, but we didn’t want to break existing setups. This is why we worked with Babel to offer a new, rewritten version of the JSX transform for people who would like to upgrade.

完全可以选择升级到新的转换,但是它具有一个 一些好处:

Upgrading to the new transform is completely optional, but it has a few benefits:

  • 通过新的转换,您可以使用JSX而不导入React .
  • 根据您的设置,其编译后的输出可能会稍微改善包的大小.
  • 它将实现未来的改进,从而减少学习React所需的概念.
  • With the new transform, you can use JSX without importing React.
  • Depending on your setup, its compiled output may slightly improve the bundle size.
  • It will enable future improvements that reduce the number of concepts you need to learn React.

这篇关于无需导入React就可以使用JSX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆