3 ms·
JSX is pure JS, unlike other templating languages.
by cercatrova 4y ago
JSX is pure JS, unlike other templating languages.
- solardev 4y agoWhat does this mean? It's certainly not vanilla JS/ecmascript. It's closer to XHTML than JS.
- cercatrova 4y agoJSX is just syntactic sugar for React.createElement [0]. This means that I can write any valid JS I want in there. For example, if I use a functional programming library with a match statement, I can have that in my JSX, I don't need to adhere to what the templating authors came up with. [0] https://fettblog.eu/jsx-syntactic-sugar/ https://fettblog.eu/jsx-syntactic-sugar/
- solardev 4y ago...but what does React.createElement compile down to? HTML and JS. Whether you do it with a clientside runtime or a serverside render, it still gets compiled or transpiled down to DOM objects in the end. I think of "pure JS" as something more like a standalone node function that takes an input and gives you some abstract data output, vs templating code whose main purpose is to define elements in the DOM. That you can intersperse with JS with DOM-like props in a JSX component (styles, states, handlers, whatever) doesn't mean that JSX isn't a templating language. It's just one that also accepts inline JS. Hell, you can do that with PHP and heredocs/template literals. Aren't all templating languages "syntactic sugar"? Isn't that their point?
- jmull 4y agoYeah, it’s (language) integrated with a convenient HTML syntax… just like all the HTML templating languages. (language) could be JavaScript as far back as 2000 as far as I know (ASP) — probably earlier and I just hadn’t heard of it.