简体   繁体   English

如何在create-react-app创建的应用程序中使用jsx文件(不运行“npm run eject”)?

[英]How to use jsx files in an app created by create-react-app (without run “npm run eject”)?

Currently I can only use .js files, in case I try to use .jsx, this will cause an error. 目前我只能使用.js文件,如果我尝试使用.jsx,这将导致错误。 Here's the problem: I usually use code formatters, and writing jsx and formatting in js files, causes the code to be completely out of order, also causing syntax errors. 这是问题:我通常使用代码格式化程序,并在js文件中编写jsx和格式,导致代码完全无序,也导致语法错误。

I am new to React, so I do not know how to configure to use jsx files, at least in other than the main files (index.js). 我是React的新手,所以我不知道如何配置使用jsx文件,至少在主文件(index.js)之外。

I've seen about the eject script, but there seem to be some drawbacks in using this. 我已经看过弹出脚本,但使用它似乎有一些缺点。

Is there any solution for using jsx files without running "npm run eject"? 有没有运行“npm run eject”使用jsx文件的解决方案?

EDIT: The error is something like, "Could not import XXX file from filename.js", that is, when I try to import from a filename.jsx file, it searches only for filename.js files, and as there is no file. 编辑:错误是这样的,“无法从filename.js导入XXX文件”,也就是说,当我尝试从filename.jsx文件导入时,它只搜索filename.js文件,因为没有文件。 js, I can not care. js,我无所谓。

EDIT2: As said in a comment, just reboot the server, and I think I was changing from js to jsx when the server was already running, now I've changed and rebooted and it seems to be working EDIT2:正如评论中所说,只是重新启动服务器,我认为当服务器已经运行时我正在从js更改为jsx,现在我已经改变并重新启动它似乎正在工作

.jsx works out of the box with create react app. .jsx开箱即用的创建反应应用程序。

You are changing the extensions while the server is running, so the server thinks that you are referencing a file that does not exists anymore since it resolves the file as .js initially and keeps that extension, is like deleting a file and trying to import it, the same error is displayed, reboot the server and everything should work as expected. 您正在服务器运行时更​​改扩展名,因此服务器认为您正在引用一个不再存在的文件,因为它最初将文件解析为.js并保留该扩展名,就像删除文件并尝试导入它一样,显示相同的错误,重新启动服务器,一切都应按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM