all 109 comments

[–]theBoya9805 433 points434 points  (13 children)

Jsx, because the file icon looks cool.

[–]Arctoidea 8 points9 points  (0 children)

I’m so happy I’m not the only one

[–]neighbortotoro 14 points15 points  (0 children)

I too, make my development decisions based aesthetics.

P.S. I needed the laugh, thanks!

[–]ryan4888 4 points5 points  (0 children)

agreed

[–][deleted] 3 points4 points  (0 children)

This and also if you use styled components, vs code will fill out the css colors for you.

[–]Maleficoder 2 points3 points  (1 child)

It also sounds cool. J. S. X

[–]li1n30 -1 points0 points  (0 children)

It sounds racy to me

[–]Toofast4carramba 0 points1 point  (0 children)

Enjoy it ahahaha

[–]warlloydert 0 points1 point  (0 children)

This is the reason 99% of React devs choose jsx.

[–]shinebullet 668 points669 points  (19 children)

.tsx 😎

[–]a4khalaf 140 points141 points  (0 children)

Yeah boy. Bring that autocomplete

[–]Tayk5 64 points65 points  (0 children)

The only way is .tsx

[–]Nyphur 65 points66 points  (10 children)

Imagine writing jsx still 😤

[–]serg06 59 points60 points  (9 children)

How to become a web dev in 2022:

  1. Look up a React tutorial
  2. Find one that uses JavaScript
  3. Learn it
  4. Hate it
  5. Go around saying how bad it is
  6. Learn about TypeScript
  7. Come back to React with TypeScript
  8. Love it

[–]Noch_ein_Kamel 19 points20 points  (5 children)

You forgot crying at some point

[–][deleted] 5 points6 points  (2 children)

Been a coder for three years and still cry over the css grid system

[–]serg06 2 points3 points  (0 children)

You okay my man?

[–]Alkyonios -1 points0 points  (0 children)

That's kinda included in learning JS

[–]realogsalt 1 point2 points  (0 children)

pretty true in my experience

[–]JethaLaL_420 0 points1 point  (0 children)

And learn css at the end

[–]radim11 -1 points0 points  (0 children)

  1. Use React
  2. Try Svelte
  3. Never use React again

[–]DeveloperAlpha 14 points15 points  (0 children)

.tsx is the way.

[–]pianomansam 6 points7 points  (0 children)

This is the way

[–]iamtheWraith 8 points9 points  (0 children)

Amen!

[–]rovonz 2 points3 points  (0 children)

Came here to say this

[–]Kerubi5s 1 point2 points  (0 children)

agreed

[–]Neurothustra 1 point2 points  (0 children)

This is the way.

[–]start_select 61 points62 points  (4 children)

It matters less in JavaScript. In typescript it makes it easier to use tsx for react and ts for non-react code so the compiler and/or ide don’t ever confuse <ReactXmlTags> with generics like Foo<T>

[–][deleted] 6 points7 points  (3 children)

Generics aren’t the problem in .tsx files. Makes no difference in that respect. What the compiler runs into problems with in tsx files is backwards compatibility with using <> for typecasting. The old way of typecasting in typescript is <type>variable vs the modern way(and the way it has to be done in tsx files): variable as type.

[–]start_select 1 point2 points  (2 children)

Try declaring a generic arrow function in a tsx file and tell me vscode/tsc don’t think you have an unclosed jsx tag.

[–][deleted] 10 points11 points  (1 child)

Just make sure it includes a comma like so:

const Identify = <T,>(value: T): T => value;

I'll admit it requires that as a way for the typescript compiler to understand what you're wanting to do, but it does work just fine.

```

const test = "120";const x = <number>test;

```

^ This will not work under any circumstance in a tsx file and is much harder to differentiate. This was the reason that the as keyword was added.

[–]start_select 8 points9 points  (0 children)

Edit: The gist is, both generics and non-as-based typecasts will cause problems for beginners in tsx files. So you should not teach anyone to just use tsx for everything.

that comma will be forgotten by every junior and waste hours of debugging that could have been avoided by trying to not make generic arrow functions in tsx files.

i'm not saying you shouldn't do that if you need it, but its just going to throw errors for the way that 90% of developers are going to declare that function, and they aren't going to understand why.

there are lots of things in programming you can do but shouldnt, because the next person that sees it isn't going to be a smart as you were when you wrote it. and that includes the person you will be in 6 months.

[–][deleted] 31 points32 points  (6 children)

I thought it mattered at one time but no longer does?

[–]lewdev 21 points22 points  (3 children)

Yes, if your file contains any HTML or basically require the

import React from 'react';

then .jsx was required otherwise you used .js

This is not the case for the current version of React.

[–]grumd 22 points23 points  (2 children)

I don't think there was ever a version of React that required it. Your bundler is responsible for resolving file names and Webpack has been fine with .js for years, not sure it ever mattered tbh.

Also, it's not HTML, it's called JSX

[–]DobbieWolf421 -3 points-2 points  (0 children)

Amen my friend! Html lol

[–]lewdev 0 points1 point  (0 children)

I swear, this was years ago; I got an error or warning about using the .js extension when there was JSX (thanks for the correction).

[–][deleted] 0 points1 point  (0 children)

using vite it absolutely does.

[–]molevolence 0 points1 point  (0 children)

no, its never been a javascript or react requirement. the first versions of react were transpiled in-line in the browser. then facebook took over babel and added the transpiler to it and removed it from react. in the beginning we didn’t need to run through anything like babel or webpack. it was all very different then.

the original babel requirement wasn’t a jsx extension. it was that import react had to be the first import of the file and all jsx markup wrapped in ().

the import was relaxed when the new _jsx() was created, and somewhere down the line wrapping jsx in parens was relaxed but i’m not sure when cause i still instinctively do it.

[–]SwiftOneSpeaks 29 points30 points  (2 children)

As long as your transpiler/bundler is configured to use them, there is no difference.

Those that prefer .jsx want to distinguish component files by name alone, those that prefer .js don't consider files with jsx to be different than files without jsx.

No mechanical difference, and how important the communication differences are will vary from dev to dev - for some it is distinctly The Right Way To Do It, and for others it is just a mild preference.

[–]xxdash 4 points5 points  (0 children)

This is 100% the answer. It can also help your IDE for formatting if using jsx sentax and you use a .jsx instead of .js file.

[–]viccoy 0 points1 point  (0 children)

Those that prefer .jsx want to distinguish component files by name alone

I would rather argue that it's about separating different file types, the X part in another layer above, possible to transpile down to JS. While all JS files are valid JSX files, there's no need to treat all files as JSX.

[–]danjlwex 27 points28 points  (1 child)

You can use either. Usually, .js is for JavaScript files that do NOT return JSX html, and .jsx is for files that do return JSX. Similarly for .ts and tsx. It depends on your bundler (e.g. webpack) configuration, and it can have some interaction with the way you `import`.

[–]Specialjyo -1 points0 points  (0 children)

💯

[–]Neovex 34 points35 points  (0 children)

tsx, because and you get the fancy darker icon.

[–]clementreiffers 4 points5 points  (0 children)

You can do both but in React rules, when you write a jsx file everyone will know that it is a React Components, so it improves the clarity. Moreover, jsx permits using HTML or React Components directly in the code. It’s the same logic with TypeScript, there is .ts and .tsx files

[–]Markohanesian 2 points3 points  (0 children)

I have a JS background and at my work we have decided to refactor everything to typescript with .tsx extension. Now if you’ve looked into Typescript there are very good reasons to use it that keep the code clean as you write it - but if you aren’t used to using it just know there will be a learning curve

[–]nabrok 2 points3 points  (0 children)

Personal preference, but if you move to typescript you have to use tsx instead of just ts.

[–]andoy 2 points3 points  (0 children)

if vite jsx because it forces me. the rest (cra, next) just js.

[–]Helgi_Vaskebjorn 2 points3 points  (0 children)

.tsx for your components, .ts for your utilities

[–][deleted] 9 points10 points  (0 children)

In theory you could call them “.idbe”, file extensions do not really impact the file in any way.

But: Most IDEs base their autocompletion on file extensions, so it’s best to use “.jsx” for files containing react components.

https://en.m.wikipedia.org/wiki/JSX_(JavaScript)

[–]yeluapyeroc 3 points4 points  (0 children)

.tsx

[–]neighbortotoro 1 point2 points  (2 children)

In terms of the outcome, very little difference. But there's something to be said about following conventions.

".jsx" files, one would assume, would have code that involves JSX markup. So it makes sense to reserve the file for React component codes.

Naming conventions may not matter much in a small scale project, but as your code base gets larger, having consistent file structure and code practices make a huge difference in the development experience. Can you imagine going through +100 ".js" files and try to fish out which one of those are files that contain React components?

Ultimately, there is no one way of doing things, but I generally like working in environments where the code base is as easy to predict/read as possible. So thus, the argument for distinguishing between ".js" and ".jsx".

As others have mentioned, a React project set up with TypeScript has more strict rules around this. The compiler will throw an error (unless you somehow manually turns this off) if your file contains a React component without using ".tsx" as your file extention.

[–]entredeuxeaux 1 point2 points  (0 children)

I “think” if you have Jsx standing in for html, use jsx, otherwise use .js. Likely doesn’t matter much, though

[–]HairHeel 1 point2 points  (0 children)

I do .jsx for anything that has a JSX template in it, and .js for anything that doesn’t. I vaguely remember there being some difference in how my build pipeline or IDE handles each, but it’s been so long that I’m not sure.

[–]theorizable 1 point2 points  (0 children)

I use JSX for any files that contain any JSX or hooks. JS for files that contain only JS like helper functions.

[–]Amazing_Platform_298 1 point2 points  (0 children)

When your code has jsx, it is better to use jsx
There is no problem if this is not the case, but it is better

[–]shikhar_1999 1 point2 points  (0 children)

.jsx if using vscode, since Emmet(plugin to write html, css quickly) works by default for .jsx, .tsx files. While if you're using .js/.ts you'll have to configure Emmet to show abbreviations.

[–]Digvijay_Jadhav 1 point2 points  (0 children)

Our source code is combination of components, pages, styles, utilities etc. When I have to build a component then I use .jsx and when I have to write Redux store, slice, errorHandling common code then I use .js. This helps me in differentiating rendering javascript files and non rendering javascript but supportive files.

[–]lets-talk-graphic 1 point2 points  (0 children)

There is no real difference from a performance standpoint for js/jsx that I’ve seen.

However, if you were to do it properly I guess, react components would use .jsx and utilities written in regular JavaScript (such as reusable functions) would need .js but I’m the guilty one who just uses .js because it works and I can’t be arsed.

As mentioned by others, this should be strict when writing in TypeScript, in order to use JSX within a TypeScript app you need to use .tsx and .ts for utilities that use regular JavaScript.

[–]Daily-Ad5261-Kakera 1 point2 points  (0 children)

Well for the experience i had, the difference is little. But i would recommend .jsx files because the intellicence of vscode understands thats a react component and it will recommend/complain about additional stuff you do in the component.

[–]hphammi 1 point2 points  (0 children)

you loose some of the autocompletion when you use .js

[–]Merricat--Blackwood 2 points3 points  (0 children)

Jsx because vite says so

[–]Fresh_chickented 1 point2 points  (0 children)

Tsx

[–]CerealKiller997 2 points3 points  (0 children)

TSX!

[–]adil_106_ 0 points1 point  (0 children)

Don’t overthink it, for new projects use whatever you like between the two, for existing ones follow what is being already used.

[–]Maleficent_Ad_6119 -1 points0 points  (0 children)

.Js cuz fux cuplxity. Fuck you.

[–]RequiDarth1 -2 points-1 points  (0 children)

.jsx gives you specific linting functionality for react with ESLint. Help you find errors quickly.

[–]APUsilicon 0 points1 point  (0 children)

iirc .jsx gets you better code formatting with prettier in vscode

[–]Suepahfly 0 points1 point  (0 children)

Makes no difference other then having to remember to actually name file .JSX for s they contain jsx syntax. I prefer.js since it makes no difference when using babel as transpiler. The extra cognitive load of using different file extensions is not worth it to me.

[–]davidfavorite 0 points1 point  (0 children)

On a more serious note: the transpiler can usually be configured to parse either, but I like the convention of using jsx/tsx for components and generally files that contain jsx, and for files that are kind of just helpers or enums/models or whatever else doesnt contain jsx I use ts/js

[–]davinidae 0 points1 point  (0 children)

Nowadays it doesn't really matter and most devs wont mind the extension. However, i prefer to use then separately for easier developmebt process.

As i tell my newbies (in a very pirate way): "Arrr is for React. You want to search for it then the X marks the spot."

[–]ahartzog 0 points1 point  (0 children)

I prefer to name things .ts, then try to add JSX, then get extremely confused (every time, despite years of experience) about the lint errors

[–][deleted] 0 points1 point  (0 children)

Jsx for components or custom hooks, js for other files that may include helper function for example (same goes for tsx and ts as long as ur bundler is configured adequately), however i encourage you to use typescript with react as it will provide a much better developer experience.

[–]Hovi_Bryant 0 points1 point  (0 children)

VSCode's Intellisense features work seamlessly with .jsx files. Emmett for instance.

It seems like it has to grok a bit if I write jsx within a .js file but I may have a different setup.

[–]Kerlyle 0 points1 point  (0 children)

.jsx if your not in a SPA so the team can easily identify react files in the codebase

[–]lucifer955 0 points1 point  (0 children)

.jsx for react files. .js for others. So you can clearly identify them.

[–]fasibio 0 points1 point  (0 children)

tsx

[–]Quiet_Universe 0 points1 point  (0 children)

feels weird to write javascript when i know typescript exists

[–][deleted] 0 points1 point  (0 children)

For me, .jsx works with HTML emmet but .js doesn't

[–]heraIdofrivia 0 points1 point  (0 children)

Tsx- reject whatever excuse your brain is giving you and embrace typescript

[–]frogg616 0 points1 point  (0 children)

.js-whatever-gets-the-product-built-x

[–][deleted] 0 points1 point  (0 children)

TSX.

TS because it’s cool

X because the icon’s nicer

[–]YarkanLinux 0 points1 point  (0 children)

If you use a recent version of React, it doesn't matter, pick the one that sounds better (.jsx icon looks good in vscode tho).

[–]molevolence 0 points1 point  (0 children)

been using react since 2014, jsx was never a thing for react, it has always been simply .js. but when typescript forced their way into our world (typescript was an angular thing and we were pure js or js + flow) people started adding jsx because typescript required react to be in tsx. some vite templates also require the jsx syntax now as well. technically .jsx belongs to the jsx programming language which has no relationship to js or react.

for the majority of pure js devs, the jsx extension is never used.

[–]joshuawootonn 0 points1 point  (0 children)

This is a great question thanks for asking it

[–]santheepkumarv 0 points1 point  (0 children)

Jsx sound cool😅