What is the technical difference between .jsx and .js files

JavascriptReactjs

Javascript Problem Overview


I would like to understand the technical difference between them so as to determine the appropriate one to use.

I've realised that they can both have same content and sometimes used interchangeably in some projects.

How does .jsx files differ from .js?

Javascript Solutions


Solution 1 - Javascript

Update for Newer Users

The JSX Compiler tool has been removed as JSXTransformer has been deprecated. The React Team recommends using another tool such as the Babel REPL.


If you wish to keep the JSX source code intact for better maintainability, I would keep them as .jsx files. Using the JSX Compiler, either manually or via build script, will convert your JSX syntax to normal JavaScript files.

Note: It is possible to serve JSX files in your production environment but React will give you console notices about reduced performance.


Personally, I would use something like gulp-jsx or gulp-reactify to convert the files.

Example with gulp-jsx:

var gulp = require('gulp');
var jsx  = require('gulp-jsx');

gulp.task('build', function() {
  return gulp.src('path/to/*.jsx')
    .pipe(jsx())
    .pipe(gulp.dest('dist'));
});

Solution 2 - Javascript

Checkout this discussion: https://stackoverflow.com/questions/46169472/reactjs-js-vs-jsx JSX is neither JS nor HTML, so giving it its extension helps indicate what it is. You can find some more discussions and docs linked there.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKosmetikaView Question on Stackoverflow
Solution 1 - JavascriptrxgxView Answer on Stackoverflow
Solution 2 - JavascriptVaishaliView Answer on Stackoverflow