Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJS - .JS vs .JSX

There is something I find very confusing when working in React.js.

There are plenty of examples available on internet which use .js files with React but many others use .jsx files.

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.

So what is the actual difference between .js and .jsx ?

like image 499
confusedDeveloper Avatar asked Sep 12 '17 06:09

confusedDeveloper


People also ask

What is the difference between .JS and .JSX in React?

JS is standard javascript, JSX is an HTML-like syntax that you can use with React to (theoretically) make it easier and more intuitive to create React components. As the docs say, the only purpose is to make it easier to create React components... there's not much else there.

What is a .JSX file?

What is JSX? JSX stands for JavaScript XML. JSX allows us to write HTML in React. JSX makes it easier to write and add HTML in React.

What is the difference between JavaScript and react JS?

Reactjs is an open-source JavaScript library for building web applications UI(User interface). React js course is exclusively in charge of the application's layered architecture.

Why we use JSX instead of JS?

It is called JSX, and it is a syntax extension to JavaScript. We recommend using it with React to describe what the UI should look like. JSX may remind you of a template language, but it comes with the full power of JavaScript. JSX produces React “elements”.


2 Answers

There is none when it comes to file extensions. Your bundler/transpiler/whatever takes care of resolving what type of file contents there is.

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.

There's a good discussion here available for read

like image 100
Henrik Andersson Avatar answered Sep 22 '22 06:09

Henrik Andersson


In most of the cases it’s only a need for the transpiler/bundler, which might not be configured to work with JSX files, but with JS! So you are forced to use JS files instead of JSX.

And since react is just a library for javascript, it makes no difference for you to choose between JSX or JS. They’re completely interchangeable!

In some cases users/developers might also choose JSX over JS, because of code highlighting, but the most of the newer editors are also viewing the react syntax correctly in JS files.

like image 26
marpme Avatar answered Sep 19 '22 06:09

marpme