ReactJS - .JS 与 .JSX
在使用 React.js
时,我发现有些东西非常令人困惑.
There is something I find very confusing when working in React.js
.
互联网上有大量使用 .js
文件和 React
的示例,但许多其他示例使用 .jsx
文件.
There are plenty of examples available on internet which use .js
files with React
but many others use .jsx
files.
我已经阅读了 JSX
文件,我的理解是它们只允许您在 JavaScript
中编写 HTML
标签.但是同样的事情也可以写在 JS
文件中.
I have read about JSX
files and my understanding is that they just let you write HTML
tags within your JavaScript
.
But the same thing can be written in JS
files as well.
那么 .js
和 .jsx
之间的实际区别是什么?
So what is the actual difference between .js
and .jsx
?
没有涉及文件扩展名的问题.您的打包器/转译器/任何负责解析文件内容类型的工具.
There is none when it comes to file extensions. Your bundler/transpiler/whatever takes care of resolving what type of file contents there is.
然而,在决定将什么放入 .js
或 .jsx
文件类型时,还有一些其他的考虑因素.由于 JSX 不是标准的 JavaScript,人们可能会争辩说,任何非普通"JavaScript 的内容都应该进入它自己的扩展,即 .jsx
用于 JSX 和 .ts
用于例如打字稿.
There are however some other considerations when deciding what to put into a .js
or a .jsx
file type. Since JSX isn't standard JavaScript one could argue that anything that is not "plain" JavaScript should go into its own extensions ie., .jsx
for JSX and .ts
for TypeScript for example.