7 ms·
That's precisely my issue with React though - JSX is a DSL that looks almost like normal HTML, but differs in small uncanny-valley ways. Sure, it's fine once yo
by aposm 4y ago
That's precisely my issue with React though - JSX is a DSL that looks almost like normal HTML, but differs in small uncanny-valley ways. Sure, it's fine once you're used to it. But it's very much not standard HTML.
- mejutoco 4y agoYes, but jsx is compiled/transpiled/transform, so it is never exposed in the served html. The end-user just sees normal html.
- j45 4y agoFeels a like a lot of work sometimes to still output html at the end of the day. Other times not so much.
- nickbauman 4y agoI was just going to say this too: JSX is a DSL. And a particularly screwy one!
- k4rli 4y agoWhat's screwy about it? In my experience it's been painless to switch between html+vanillajs and React projects.
- nickbauman 4y agoJSX has an Async "tag"... Let that sink in. <Async></Async>
- afavour 4y agoJust one example: the class attribute in HTML vs className in JSX. It’s not that it’s complicated, you just use one or the other. But it’s very indicative of what’s going on under the hood: despite looking exactly like HTML, JSX isn’t creating HTML. It’s creating elements via JavaScript APIs.
- eyelidlessness 4y agoFWIW, the className prop is a React thing not a JSX thing. Other libraries which use JSX will happily accept a plain class prop. The React limitation is abstraction leakage: props are not attributes, they map to DOM properties. But to the point that JSX is a DSL, that limitation is specifically because React itself is very tightly coupled to DOM semantics… but JSX explicitly has no built in semantics[1]. 1: First sentence of https://facebook.github.io/jsx/ https://facebook.github.io/jsx/ - “JSX is an XML-like syntax extension to ECMAScript without any defined semantics.”
- crubier 4y agoNarrative violation: className and friends are NOT a React thing! They are a standard DOM property! https://developer.mozilla.org/en-US/docs/Web/API/Element/className https://developer.mozilla.org/en-US/docs/Web/API/Element/cla... The only "debatable" thing that React does is to use the names from the JS API in JSX (where some people would expect names from the HTML API because JSX looks like HTML)
- afavour 4y agoThat’s basically what I said, though? The point is that JSX is made to look like HTML so you think you’re writing HTML but you’re not, you’re using a JS API. Also I had to laugh at the sibling replies: > Narrative violation: className and friends are NOT a React thing! > FWIW, the className prop is a React thing not a JSX thing Don’t try to say it doesn’t cause confusion!
- afavour 4y agoI strongly agree but looking at the Alpine examples it feels like the worst of both worlds to me. At least JSX allows code checking etc, putting code inside HTML attributes has a real ick factor for me.
- idoubtit 4y ago> That's precisely my issue with React though - JSX is a DSL React does not require JSX, so it's a bit strange to raise the same issue with JSX as with the DSLs of Vue, etc. If one wants to avoid JSX, it's always possible to write pure JS: return (<h1 className="sc">Hello {title}</h1>); return React.createElement('h1', {className: "sc"}, `Hello ${title}`);
- savy91 4y agoYou are still writing JSX in a string, aren't you?
- henryfjordan 4y agoReact at it's core is a function (or set of functions) that interpret strings like "h1" into DOM elements in a browser. JSX is a shorthand for writing those function calls. <h1> becomes React.createElement("h1"). Sure you are still largely using HTML element names, but that's only because people don't really use JSX for anything else. The whole point is that it's easier to write JSX than the underlying function calls. You can use JSX to target things other than DOM by supplying your own function instead of React.createElement(). Here's an example of an express web-server using JSX: https://betterprogramming.pub/how-to-create-a-web-server-with-jsx-1b3da3704502 https://betterprogramming.pub/how-to-create-a-web-server-wit...
- mostlylurks 4y agoNo, you're not. There is no JSX of any sort in the example the parent commentor provided, nor in the approach that they present, even outside of that specific example. The first parameter is the component, which is string containing the HTML tag name for built-in components which are basically just HTML elements, such as div, span, img, h1, etc, but the actual component (a function or a class) for any other type of component. The second parameter is the props. The third parameter is the children, which happens to be a string in this case because the content of the h1 in the example is pure text, but if you'd want anything more complicated as children, you couldn't put in a string and pass that, you'd provide it more React.createElement -provided values.
- 4y ago
- frankfrank13 4y agoreact doesn't actually use/require JSX right? just the typical way of writing react does (or at least this used to be true)
- spiderice 4y agoI mean technically you're right.. but it's more useful to talk about it in the way that basically everyone is using React.. with JSX.
- FlorianRappl 4y agoThat is just wrong on many levels. JSX brings an XML-like notation to JS. It has nothing to do with HTML. react-dom couples your React DOM to the real DOM - thus allowing you to use special JSX elements that create DOM elements. But then you / React interacts with them using the DOM API - not HTML source code. Things like `htmlFor` or `className` should not be confusing - these are the official DOM properties. `style` in its DOM API also has an object-oriented API and not a string. If you are confused by these things (className vs class, ...) then potentially you started learning JSX from a completely misleading / wrong point. I know most articles on React / JSX get that wrong, but this non-sense has to stop. After all, you do not write React in the browser because you want to generate HTML - you do write it to manipulate the DOM. On the server, you may want to generate HTML and then this can be misleading (true), but this has not been the main objective.
- cooperadymas 4y agoTelling someone they learned something wrong maybe isn't the best approach when trying to explain why something should not be confusing. Especially when it's a widely held view, maybe the learners are not the problem. Most people learn JSX when they learn React. It was created by Facebook for React, after all. What does the React docs teach about className? https://reactjs.org/docs/introducing-jsx.html https://reactjs.org/docs/introducing-jsx.html Here in the intro it doesn't even explain that you need to use className instead of class. It uses it with no explanation. Then it throws in a line about camelCasing and assumes that you understand why it changed. Surely the JSX in depth page explains it. https://reactjs.org/docs/jsx-in-depth.html https://reactjs.org/docs/jsx-in-depth.html It does even less. https://reactjs.org/docs/dom-elements.html#classname https://reactjs.org/docs/dom-elements.html#classname Finally! In a page that seemingly has nothing to do with JSX we get told to change how we write HTML, but we're not even told why. (The htmlFor section tells us.) No wonder people are confused. Yes, className is a property of the Element interface but it is not the attribute used when writing HTML. You're still changing how you write HTML. It is no longer standard. That is the point people are making here - JSX introduces enough edge cases that you must learn that it adds equally as much mental overhead as the template DSL for Alpine or Vue. Even if people understand the reasoning and it is not confusing, it's still a shift.
- tshaddox 4y agoI’d be totally fine with using React without JSX, other than the slight hassle of losing all that muscle memory from years of using JSX.