Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I have to save react component files with a jsx extension

I've been writing react for a few months now and I just realized that some of my files have a .js extension while others have .jsx extension. When I write jsx in the .js files, everything still works. Does it matter what the extension is?

by the way (for context), I'm using webpack to generate a bundle.js file. Does that affect anything?

like image 262
duxfox-- Avatar asked Apr 27 '16 18:04

duxfox--


People also ask

Do I need to use JSX extension?

JSX converts HTML tags into react elements. You are not required to use JSX, but JSX makes it easier to write React applications.

Is it mandatory to use JSX in 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.

What is JSX file extension?

A JSX file is a JavaScript (. JS) file written using JSX syntax. It contains code that is most likely part of a single-page or mobile application. JSX files can be opened in any text editor, but are meant to be opened in source code editors.

What is the difference between JSX and JS file?

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.


2 Answers

No using .js or .jsx doesn't matter since you have webpack/babel to transpile everything. Really the main difference is when you import files in, you have to include .jsx extension for jsx files where if it is just a js file, you can just put the file name. Ex : import File from './file.jsx' vs import File from './file'

like image 188
erichardson30 Avatar answered Sep 23 '22 02:09

erichardson30


As already mentioned, technically it doesn't matter.

But, especially when it comes to collaborative projects, it's may be interesting to check the Airbnb React/JSX Style Guide which is mentioning:

Extensions: Use .jsx extension for React components.

Source: https://github.com/airbnb/javascript/tree/master/react

like image 32
to7be Avatar answered Sep 20 '22 02:09

to7be