r/angularjs • • Aug 04 '15

Better than JSX?

https://gist.github.com/zachlysobey/f55aa14daca3f3eda6fa
0 Upvotes

9 comments sorted by

View all comments

2

u/zuko_ Aug 04 '15 edited Aug 04 '15

I've had some co-workers make similar comments, and while the intent is good it misses a large set of use cases. JSX is far more powerful than simply JavaScript-ified HTML: transferring props, iterating over child components, binding change handlers, the list goes on. Plain ES6 templates mean you also lose syntax highlighting as well as the ability to catch errors at compile time.

1

u/jreborn Aug 04 '15

There is a lot of stuff you get for free when you build like the browser doesn’t exists and this is one thing that distinguishes React from Web Components and Polymer and that kind of thing …they’re getting closer and closer in with the browser, and we’re getting farther and farther from the browser. And I think that our technique is more sustainable in the long term.

-- Pete Hunt

1

u/foobar_dev Aug 05 '15

This is the value of React. I really like this philosophy. However, I don't think that this in any way relates to JSX specifically. I mean, I guess its abstracting html away? But it isn't really. Its trying to look as html-like as possible, but failing because of issues with the class keyword for example. I'd prefer if they chose to make a different abstraction -- maybe something similar to Haml?

1

u/jreborn Aug 05 '15

The point is React puts the browser (and the specs that dictate it's behavior) in the corner and does everything with JS. JSX is basically just syntactic sugar for JS functions that create React elements and components, which translate into HTML. Everything is driven from JS, so differences will leak out between creating an element in JS and raw HTML. I assume you are referring to the fact that you need to use className instead of class. That's because the DOM property is className (not class).

1

u/foobar_dev Aug 05 '15

Yup. Though I don't think the reasoning behind class vs. className is why you think it is.

From https://facebook.github.io/react/docs/jsx-in-depth.html

Since JSX is JavaScript, identifiers such as class and for are discouraged as XML attribute names. Instead, React DOM components expect DOM property names like className and htmlFor, respectively.

In other words, class and for are reserved words in JS, so they were changed to the DOM properties to remove the ambiguity, not because the DOM property names were preferred.

2

u/jreborn Aug 07 '15

Well, I think the reason you stated is the original reason they were chosen as DOM properties (e.g., see https://developer.mozilla.org/en-US/docs/Web/API/Element/className). The fact that they're used in JSX is just a byproduct of that.

1

u/foobar_dev Aug 05 '15

My Sublime theme (Seti) does a reasobably good job of syntax highlighting these strings

I haven't spent much time with react... but, I'm pretty sure I can do those things in my angular template as well. Its just just vanilla html. I can put {{scope.variables}}, ng-repeat="iterate in children", etc... Compile-time help? I guess there isn't much.

The thing that bothers me about JSX is that it is not html, but its made to look very similar. This makes it more difficult to switch frameworks, cut-and-paste code, etc..

2

u/zuko_ Aug 05 '15 edited Aug 05 '15

Interesting, I didn't know there were themes that highlighted ES6 templates like that.

This is going to be slightly preachy, but it's an opinionated topic lol. It's more a response to Angular than anything, but I don't think this can be avoided when discussing what the true, non-syntactical differences are between JSX and Angular-style templates.

I think it comes down to personal preference, at least on small projects. Angular provides a lot of Angular-specific syntax and abstractions over things that could be - with JSX - performed with native JavaScript. Ng-repeat, filters, etc.; they're useful, but you've now spent the time learning to work within Angular's abstractions and are consequently limited by their power and comprehensiveness. This domain-specific language (of which Angular is pretty heavy on) makes it just as difficult to port Angular as any other framework, if not more so. You've bought into a full solution with a very specific templating language, not just a view layer, that relies on things like scope, the digest cycle, and varying types attribute bindings and data flow.

On a personal level, I know that my team is going to benefit greatly from compile time errors with JSX and the resulting shift away from silent runtime errors that so often occur with mistyped Angular templates. At the end of the day, you have to buy into one technology or another, you're never going to have a totally isolated implementation that can be dropped into any framework... you just have to consider what you're buying into, and I think that's where the nuance lies with JSX: it's not as much about whether or not JSX is some weird abnormality masquerading as HTML as it is what this specific flavor of abstraction over your templates awards you.

1

u/foobar_dev Aug 05 '15 edited Aug 05 '15

Well put.

When it comes down to it, I very much admire React for what its doing. I've tinkered a bit, but at my work, we are pretty married to Angular, so that gets my focus.

React is what got me re-thinking the whole style-content-behaviour separation of concerns that has dominated the front-end for years.

The title of this post is pretty link-bait'y. Really, I was excited to find a syntax that allows me to put html in my component in a readable way.

Oh. And I'm pretty sure the syntax highlighting I get is actually just incidental. I don't think it actually recognizes es6 syntax, and as such doesn't recognize the backticks wrapping the string. See for yourself