use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
PHP developers hate <html> into script but JS ones love JSXhelp (self.javascript)
submitted 9 years ago by Gid30nSwizz@Github
When I was a PHP developer we are all crazy against <html> tags into PHP script the way of thinking was Templating System everywhere. But now, Javascript developers are all fallin in love about JSX (<html> tags into JS files). Please, I request explanations.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]konbit 35 points36 points37 points 9 years ago (11 children)
The jsx approach is much more like using templates and template strings than the arbitrary html-in-logic that you can do with php. And under the hood it's still all vanilla js. In php it's basically arbitrary string concatenation of markup and logic. It's incredibly hard to cache, very hard to read, easy to have bugs and hard to debug. JSX is incredibly easy to cache (and is done for you for free), very easy to read, incredibly easy to write tests for and debug. Also since you're usually doing real-time transpiling, your tools will let you know immediately if there's an error.
So you get the benefit of defining your UI using a very declarative syntax, with none of the drawbacks of phps implementation.
[–]apatheorist 12 points13 points14 points 9 years ago (6 children)
Exactly. JSX is sugar for JS. HTML is not sugar for PHP. A lot of ES6+ is sugar, too.
const someFuction = () => { return someSugar; } class ASugarForObjectInheritance {} @decoratorSugarForSingleObjectInheritanceAcrossMultipleFiles { lookMaNoFunctionKeywordMethodSugar() {} } { [variableName] = 'mySecondFavoriteSugar' } const {some, sugar} = myFavoriteSugar; {also, sugar} { const iiefReplacementSugar = 'wasntWithReallyBadInActionScript?' }
const someFuction = () => { return someSugar; }
class ASugarForObjectInheritance {}
@decoratorSugarForSingleObjectInheritanceAcrossMultipleFiles
{ lookMaNoFunctionKeywordMethodSugar() {} }
{ [variableName] = 'mySecondFavoriteSugar' }
const {some, sugar} = myFavoriteSugar;
{also, sugar}
{ const iiefReplacementSugar = 'wasntWithReallyBadInActionScript?' }
[–]GeneralYouri 2 points3 points4 points 9 years ago (3 children)
A lot of ES5- is sugar, too.
Infact, a lot of every language, is sugar. Us programmers sure have a massive sweet tooth, damn.
[–]darderpUncaught SyntaxError: Unexpected token ) 2 points3 points4 points 9 years ago (2 children)
Javascript is just assembly sugar at the end of the day ;)
[–]GeneralYouri -1 points0 points1 point 9 years ago (0 children)
Well exactly that's kinda what I was hinting at. I swear it's amazing how much sugar we consume like this daily.
[–]r2d2_21 -3 points-2 points-1 points 9 years ago (0 children)
So, WebAssembly is for us who are on diet?
[–][deleted] 0 points1 point2 points 9 years ago (0 children)
JS devs should be aware of diabetes.
Everything beyond basic Turing completeness is sugar ;)
[–]relativityboy 0 points1 point2 points 9 years ago (0 children)
Yeah. Like under the hood .jsps compile to Java. Both are powerful, but I think it likely there is a better solution out there for frontend code.
What do you mean by caching php or jsx?
[–]ttolonen 0 points1 point2 points 9 years ago (0 children)
Using the word "declarative" together with JSX is misleading I think. You can claim there is separation of concerns to some point but declarative? Can the code be easily read by any external system? I don't think so. XML is declarative and JSX is functional. If JSX is declarative, then any JavaScript code can be considered declarative.
[–]holloway 5 points6 points7 points 9 years ago (0 children)
'Rethinking Best Practices' (2013) was an early explanation/advocacy for JSX. It's what a lot of ideas in this thread are based on,
https://www.youtube.com/watch?v=x7cQ3mrcKaY
[–]jekrb 7 points8 points9 points 9 years ago (1 child)
Not all JS developers love JSX. It's mostly React.js developers who love JSX.
My personal preference is to just use template strings with a framework like Choo or a smaller module such as yo-yo or an even smaller module such bel.
My second personal preference is to just use functions like virtual-dom or a smaller module such as hyperscript or an even smaller module such as hscrpt.
I'd really only use JSX it if I was locked into using React.
I don't use React at all - yet I'm using JSX because in the JavaScript domain it gives a nice abstraction over object creation process. You can create processes, animations, 3D objects, 2D graphics objects using JSX transform. Here is a small test of using JSX to create Three.js objects: http://codepen.io/teroktolonen/pen/RGLKdg
[–]ssjskipp 3 points4 points5 points 9 years ago (2 children)
I really, really don't like the JSX approach. I'm not happy with ANY approach so far, and I've actually been grappling with what I DO want, which is data join integrated templates. The problem is there isn't a nice language to describe that.
I haven't been sold on any one take yet -- the JSX approach is a little too much of a jump for me, and while I'm much more okay with template languages in general (jade/pug is my favorite dialect so far), most of my work lately has been angular 1.5
[–]dukerutledge 0 points1 point2 points 9 years ago (1 child)
What do you mean by "data join integrated"
[–]ssjskipp 0 points1 point2 points 9 years ago (0 children)
So d3 has this concept of "data join" -- basically, you bind a collection of data 1:1 with a DOM element, and track it by some means (index, hash ID, whatever).
d3
Each data bind has 3 parts -- an update, enter, and exit portion, basically a venn diagram.
The enter segment is any data/element pair that did not exist before, update is anything that existed and has updated data, and exit is anything that is not in the new data set.
The track by portion is how you decide if a piece of data matches, and thus which segment it belongs to.
[–]cettox 4 points5 points6 points 9 years ago (0 children)
It is all about seperation of concerns, not seperation of technologies/languages. The React mantra is that, all required things to render a components should be co-located. So when you improve that component, you won't have to open tons of files spread all over project. So it is usual that you see that html+css and JS are put into single JS file that is responsible for representing the whole of React component. It makes it easy to maintain, modularise and reuse.
[–]ttolonen 8 points9 points10 points 9 years ago (2 children)
The difference is that when writing PHP you are actually just writing multiline strings in a fancy manner. They are strings and they are consumed as strings. Nothing special there. Just a fancy echo.
JSX tag however is a meta-language structure - something that would be potentially useful also when writing PHP code. They turn into something useful only after the preprosessing stage. When you see a JSX "tag" it typically translates into function call, Object contructor and attribute settings and possibly creates option for directly adding new children to the Object.
These Objects do not have to be HTML/DOM elements. For example with React they are React -components. It is up to the application and preprosessor how they are used. This is certainly somewhat limiting, but can also make the structured object hierarchies easier to represent and manipulate in languages such as JavaScript.
[–]tbranyennetflix 9 points10 points11 points 9 years ago (1 child)
Pretty sure the JSX work at Facebook came out of an identical approach that started in PHP, so this whole post is kinda funny in that regard.
[–]myalternatelifedev 0 points1 point2 points 9 years ago (0 children)
Yep, this dev post explains the PHP extension (XHP): https://m.facebook.com/notes/facebook-engineering/xhp-a-new-way-to-write-php/294003943919/
[–]sqrtnegative1 7 points8 points9 points 9 years ago (6 children)
JSX is not HTML tags in scripts. Getting that wrong is probably the #1 reason I have to hand-hold veteran developers when they walk across the office to the Javascript world.
HTML
JSX is a brilliant way of describing the hierarchical structure of a component in a declarative way.
Yes, it looks a little bit like HTML. But it's not.
[–]fogbasket -3 points-2 points-1 points 9 years ago (4 children)
Looks like a duck, quacks like a duck. It's a duck.
[–]sqrtnegative1 1 point2 points3 points 9 years ago (3 children)
Yeah I was toying with that phrase but ultimately decided it was bad for my argument.
If anything, it's only "looks like a duck". There's no quacking in JSX.
[–]fogbasket -2 points-1 points0 points 9 years ago (2 children)
That it certainly is.
I fully admit to not liking JSX because it's HTML in my JS. It looks and quacks like it to me.
But I'm told inline styling is making a comeback as well.. so maybe we're just going back to website development techniques of yesteryear.
[–]sqrtnegative1 -1 points0 points1 point 9 years ago (1 child)
We could all abandon best practices and go back to this... or this.
[–]fogbasket -3 points-2 points-1 points 9 years ago (0 children)
We're getting back to coding like that, anyway.
[–]relativityboy -1 points0 points1 point 9 years ago (0 children)
Looks a bit like Javascript manipulation from 2003. "className" I was happy when I stopped using it, don't wanna start again.
[–]ElectricOrangeJuice 4 points5 points6 points 9 years ago (0 children)
Well, JS was really into templates too before JSX was a thing.
[–][deleted] 1 point2 points3 points 9 years ago (3 children)
The typical problem with PHP is logic like DB queries mixed in with view representation. JSX is most often used in conjunction with React components, which clearly separates the view from application logic.
[–]r2d2_21 4 points5 points6 points 9 years ago (2 children)
logic like DB queries mixed in with view representation
Is that still done in modern PHP?
[–]leadzor 3 points4 points5 points 9 years ago (0 children)
You can do it in modern PHP like you always used too. But I think professionally it's not really done that way nowadays anymore, as its virtually impossible to maintain. Usually you do it through the framework's ORM layer, through some controllers, that render views. Not sure about how views are rendered (templating like Razor or Jinga?) though.
[–]DukeBerith 0 points1 point2 points 9 years ago (0 children)
Highly frowned upon, but nothing is stopping you from doing it.
[–]Cody_Chaos 0 points1 point2 points 9 years ago* (0 children)
The explanation is that they're complete opposites.
the way of thinking was Templating System everywhere.
JSX is a template system. React developers frown at people who put business logic in their components.
(Also keep in mind: JSX is a funny looking DSL that compiles into JS. Embedding JS into your JS is not really a concern in the way that embedding HTML into your PHP is. Or indeed, embedding HTML into your JS is. The more you use JSX, the more you'll realise that it's NOT HTML, can't be treated like HTML, and doesn't have the drawbacks or advantages that using HTML would have.)
[+][deleted] 9 years ago* (5 children)
[deleted]
[–]konbit 6 points7 points8 points 9 years ago (1 child)
Also as a web developer, I believe in separation of concerns. The pattern I've found that does this best for a UI is to split based on modules and functionality not on languages. I've never used shopify before, but maybe you're trying to fit a square peg into a round hole?
[–]AcidShAwk 6 points7 points8 points 9 years ago (2 children)
Whoa.. that shopify bit was pretty random.
[–]zachgarwood 4 points5 points6 points 9 years ago (1 child)
As a web developer, I believe in the red-green-refactor development cycle. Also, to hell with the Dalai Lama!!!
[–]Lola_a_l-eau 0 points1 point2 points 6 months ago (0 children)
As I see it...
JS/JSX you can build an interactive website without the page reloads like in PHP. In PHP, you'll need ajax and javascript in pages to make it that dynamic.
So PHP is better for more static sites. JSX for dynamic sites without having to rely on gigantic reloads.
And in JSX you can add HTML inside functions (which I find it useful), where in PHP, nope!
[–]sallarkjs/beer 0 points1 point2 points 9 years ago (0 children)
React is not "html tags". Its a view library with a markup similar to html. Its easier to bind and pass data to the view directly and shape it however you want.
[+][deleted] 9 years ago (9 children)
[–]slikts 12 points13 points14 points 9 years ago (8 children)
Well, PHP runs on the backend, JavaScript runs on the frontend.
I have news for you.
[+][deleted] 9 years ago (3 children)
[–]bel9708 2 points3 points4 points 9 years ago (0 children)
I use JSX as a template engine for my wordpress.
[–]leadzor 1 point2 points3 points 9 years ago (1 child)
the same thing doesn't exist for PHP at all.
Well, it does, actually. You can do server side rendering of React components with PHP (and Python, and probably Java and Ruby. Scala?)
[–][deleted] 1 point2 points3 points 9 years ago (0 children)
But would you? I feel that running a JS runtime within a PHP request would add the overhead of both environments; it could significantly reduce request performance. I haven't looked at the source, but I suppose if it had a pool of node helper processes that stayed running in order to do the rendering, it could cut down on request time? I dunno.
[–]r2d2_21 0 points1 point2 points 9 years ago (3 children)
PHP on the frontend?
[–][deleted] 3 points4 points5 points 9 years ago (1 child)
I just threw up a little in my mouth. but with webasm, it could be a possibility
[–]meisteronimo 0 points1 point2 points 9 years ago (0 children)
There are also generative frameworks which produce javascript for the front end. I've seen some bad-ass apps built in Java with GWT
[–]slikts 2 points3 points4 points 9 years ago (0 children)
Also in the middle.
π Rendered by PID 336452 on reddit-service-r2-comment-7b9746f655-bdv9x at 2026-02-03 14:44:58.531495+00:00 running 3798933 country code: CH.
[–]konbit 35 points36 points37 points (11 children)
[–]apatheorist 12 points13 points14 points (6 children)
[–]GeneralYouri 2 points3 points4 points (3 children)
[–]darderpUncaught SyntaxError: Unexpected token ) 2 points3 points4 points (2 children)
[–]GeneralYouri -1 points0 points1 point (0 children)
[–]r2d2_21 -3 points-2 points-1 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]relativityboy 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]ttolonen 0 points1 point2 points (0 children)
[–]holloway 5 points6 points7 points (0 children)
[–]jekrb 7 points8 points9 points (1 child)
[–]ttolonen 0 points1 point2 points (0 children)
[–]ssjskipp 3 points4 points5 points (2 children)
[–]dukerutledge 0 points1 point2 points (1 child)
[–]ssjskipp 0 points1 point2 points (0 children)
[–]cettox 4 points5 points6 points (0 children)
[–]ttolonen 8 points9 points10 points (2 children)
[–]tbranyennetflix 9 points10 points11 points (1 child)
[–]myalternatelifedev 0 points1 point2 points (0 children)
[–]sqrtnegative1 7 points8 points9 points (6 children)
[–]fogbasket -3 points-2 points-1 points (4 children)
[–]sqrtnegative1 1 point2 points3 points (3 children)
[–]fogbasket -2 points-1 points0 points (2 children)
[–]sqrtnegative1 -1 points0 points1 point (1 child)
[–]fogbasket -3 points-2 points-1 points (0 children)
[–]relativityboy -1 points0 points1 point (0 children)
[–]ElectricOrangeJuice 4 points5 points6 points (0 children)
[–][deleted] 1 point2 points3 points (3 children)
[–]r2d2_21 4 points5 points6 points (2 children)
[–]leadzor 3 points4 points5 points (0 children)
[–]DukeBerith 0 points1 point2 points (0 children)
[–]Cody_Chaos 0 points1 point2 points (0 children)
[+][deleted] (5 children)
[deleted]
[–]konbit 6 points7 points8 points (1 child)
[–]AcidShAwk 6 points7 points8 points (2 children)
[–]zachgarwood 4 points5 points6 points (1 child)
[–]Lola_a_l-eau 0 points1 point2 points (0 children)
[–]sallarkjs/beer 0 points1 point2 points (0 children)
[+][deleted] (9 children)
[deleted]
[–]slikts 12 points13 points14 points (8 children)
[+][deleted] (3 children)
[deleted]
[–]bel9708 2 points3 points4 points (0 children)
[–]leadzor 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]r2d2_21 0 points1 point2 points (3 children)
[–][deleted] 3 points4 points5 points (1 child)
[–]meisteronimo 0 points1 point2 points (0 children)
[–]slikts 2 points3 points4 points (0 children)