3 ms·
> Right, but the <></> syntax still feels like a hack around a need to have just one node at the top of the three. It’s roughly analogous to an array (and in s
by eyelidlessness 3y ago
> Right, but the <></> syntax still feels like a hack around a need to have just one node at the top of the three.
It’s roughly analogous to an array (and in some implementations it literally produces an array). The “hack” isn’t around returning multiple nodes, you can’t produce (and therefore return) an expression of multiple anything without some container.
> There are many situations in HTML where you want a plain string inside the tag, like <input checked>
Some implementations do accept that (checked prop without any curly braces or prop value assignment), with roughly the same semantics (the prop is assigned a boolean true). React doesn’t accept it IIRC, but React has quite a few rules beyond JSX (as specified, and as used in other implementations).
> But inside it, which is where the JSX magic happens, you have to resort to things like:
You don’t! You only have to use an expression. This is perfectly valid JSX (assuming whatever is outside the outer curly braces is also valid):
{(() => {
if (isItTrue) {
return <i>yep</i>;
} else {
return <b>nope</b>;
}
})()}
> That's probably the biggest complement to JSX possible :) I admire Lisp from afar, but could never stand the parentheses.
It really is a pretty close analogy, not just a compliment. And in most uses, JSX is almost exactly akin to a Lisp macro (the biggest difference besides delimiters and host language semantics is that the “macro” definition is supplied by some arbitrary tooling external to the host language).
- tambourine_man 3y ago> You don’t! You’re right, but I’m not sure wrapping it in a self calling function is any better to me. It feels like a lot of contortionism to just echo strings.
- eyelidlessness 3y agoSure, that’s reasonable if your only concern is echoing strings. Part of what’s appealing to me about JSX is that it’s capable of far more than string output (even in that’s ultimately all you’re producing from it). The thing (usually) produced in a JSX expression is a value, which can be any arbitrary data structure, and can be composed in any arbitrary way you like. Echoing strings—which is to say producing output as a side effect, out of band of a component’s return value—undermines that. It’s totally a valid approach (and makes perfect sense in the context of PHP’s rendering model), it just has a different set of capabilities.