Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

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).

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"?

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. js, I can not care.

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

like image 359
marcos souza Avatar asked Jun 08 '19 18:06

marcos souza


People also ask

Can I use React without npm?

While React can be used without a build pipeline, we recommend setting it up so you can be more productive. A modern build pipeline typically consists of: A package manager, such as Yarn or npm. It lets you take advantage of a vast ecosystem of third-party packages, and easily install or update them.

Can we use JSX without React?

JSX is not a requirement for using React. Using React without JSX is especially convenient when you don't want to set up compilation in your build environment. Each JSX element is just syntactic sugar for calling React. createElement(component, props, ...children) .

How do I run an already created React app?

Open a terminal on vscode, then make sure you already node installed. Type npm install after that npm run start or whatever command to run, you can see on package. json . Save this answer.

Is JSX required to run a react JS application?

React doesn't require using JSX, but most people find it helpful as a visual aid when working with UI inside the JavaScript code. It also allows React to show more useful error and warning messages.


1 Answers

.jsx works out of the box with create react app.

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.

like image 149
ramirozap Avatar answered Oct 15 '22 10:10

ramirozap