-
Notifications
You must be signed in to change notification settings - Fork 113
Conversation
be208f2
to
c1abe1f
Compare
cc @sebmarkbage |
I’m still unsure if I’m in the knee-jerk reaction phase to this or not, but I do not like this direction. This takes a step away from the While the example tag implementations are trivial, the cognitive load they create is much larger. A components implementation is no longer simple javascript |
+1 to @iamdustan's comment |
@iamdustan Everything is still just javascript. You're still able/free to use |
<foreach element="author" index="authorIndex" collection={book.authors} /> | ||
<a | ||
href={"http://www.authors.com?id="+author.id} | ||
style={{color: 'blue', textDecoration: 'none'}}>{author.name} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm interested in how that will be compiled to JS, as a function of author
argument? How JSX compiler should decide when to compile JSX element as function and when as React element?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a couple of options for compiling JSX to JS, none of them particularly fantastic, but all workable. One would be to compile author.id
to $scope.author.id
. Another would be to use the with
operator.
It's worth noting that the expressions will probably need to be wrapped in a function/closure and executed during render instead of during element creation, but that's not a big deal, should be virtually invisible, though there is a slight performance overhead of creating/invoking the anonymous function. This can probably be minimized by checking which variables are in scope and only converting to a function if you are using variables inherited from a tag.
Regardless of the exact desugaring syntax, it's the concept of allowing tags to bind variables that is most important/interesting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reimplementation of scoping rules is why @iamdustan is saying it's not still "just JavaScript." Of course, anything you write will be "pure javascript" in the sense that it has to compile to JavaScript in the end—but it'll differ in terms of syntax and behavior, which is what adds the cognitive load.
I think the fundamental disconnect here is that your value proposition is that this would make it possible to approximate JS control structures in JSX, but much (most?) of the community doesn't see those control structure elements as something positive. Nobody is under the impression that you're advocating for the inclusion of tempting tags, but if the purpose of your proposal is to enable something they see as harmful, they're going to be against it on the grounds of enabling developers to "fall into the pit of success."
@iamdustan +1. It looks like angular with JSX notation |
@Kureev Haha, yeah, @sebmarkbage also smiled and said "looks like angular" when he first saw this. I hadn't actually looked at the angular syntax when I wrote this up (but I was inspired by Struts2, so I can't claim credit for coming up with it by myself). The fact that angular (and other frameworks) are using this pattern is an indication that it's potentially useful. We shouldn't reject it out of hand just because other people do something that looks similar :). We should decide if it's a potentially useful concept and then iterate. |
@JSFB Why you don't like the way it works now? Compute your DOM tree first (if it's needed) and compose it in the |
Perhaps a different idea is better here: allow JS between tags and make
|
@Kureev This doesn't change that behavior. This code still goes into your render function. The only difference is that you are now able to implement tags like |
You could make the regular JavaScript version more declarative as a fairer comparison: return <div>
{books.map(book => <div key={book.id}>
<h1>{book.title}</h1>
By:{' '}
{book.authors.length === 0 && <span style={{color: 'gray'}}>Unknown author</span>}
{book.authors.length > 0 && book.authors.map((author, idx) => <span key={author.id}>
<a href={"http://www.authors.com?id="+author.id}
style={{color: 'blue', textDecoration: 'none'}}>
{author.name}
</a>
{idx !== book.authors.length && ', '}
</span>)}
</div>)}
</div> I really don't like the idea of making JSX be anything but sugar for (Aside: You could use a utility function to avoid the manual return <div>
{joinElements(books.map(book => <div key={book.id}>
<h1>{book.title}</h1>
By:{' '}
{book.authors.length === 0 && <span style={{color: 'gray'}}>Unknown author</span>}
{book.authors.length > 0 && joinElements(book.authors.map(author =>
<a key={author.id}
href={"http://www.authors.com?id="+author.id}
style={{color: 'blue', textDecoration: 'none'}}>
{author.name}
</a>
), ', ')}
</div>), () => <hr/>)}
</div> |
Totally agree with @insin |
Related: JSX Control Statements: https://github.com/valtech-au/jsx-control-statements |
@jimfb (Sorry, just hijacking this briefly) http://wiki.ecmascript.org/doku.php?id=harmony:array_comprehensions could this be something to consider for JSX? I.e. statement like syntax inside braces, more readable than ternary and various other hacks while still being very much like JS. |
@syranide We don't want the syntax inside the squiggly brackets to be any non-standard javascript, so with jsx expressions as they are today, that syntax would be unlikely to fly. That said, if jsx expressions like |
Yeah ofc, but if array comprehensions make it into the standard, that line becomes rather blurred to me. If the standard considers the special syntax important enough for array comprehensions I feel that the same could reasonably apply to JSX (ternary operators are not very readable). Just bringing up because I found it interesting, not going to fight over it ;), but it doesn't seem as clear cut "non-standard javascript" in this case to me.
Considering the conditional elements usually goes deep inside the hierarchy I'm not sure how you would do this (unless you're proposing statement like syntax inside JSX?). Also, this would (unintuitively) mess up implicit indicies if used in conditionals so it would probably not be a good idea. |
Potential direction for JSX. By late-binding variables in jsx expressions, we gain a lot of flexibility and allow for a much more readable/declarative syntax.
EDIT: Some people seem to be under the mistaken impression that I'm advocating the inclusion of tempting tags (like
push
,iff
, andforeach
) into the JSX langauge. That is NOT the case. The point of this example is to make it POSSIBLE to implement components likeforeach
(and incidentally justify the use case by showing that such a tag might be desirable for making your code more declarative and therefore more maintainable). The basic tenants of JSX (including the ability to easily jump into javascript at any time) would still exist with this change. The only difference is a slight addition to the variable scoping rule.