本文介绍了无需导入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
:
完全可以选择升级到新的转换,但是它具有一个一些好处:
Upgrading to the new transform is completely optional, but it has afew 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!