It looks like a DSL, but it actually isn't - it's just regular javascript/typescript with a very thin 'declarative/markup' veneer. it basically tricks you into thinking you're writing a HTML-like UI-DSL, but it also just so happens to accept arbitrary JS expressions, because it is just regular JS.
till this day you can't use linq to render a sequence of elements. Like people.Select(p => <div>{p.FullName}</div>);
it's such a shame XAML and Razer syntax haven't evolved.
> till this day you can't use linq to render a sequence of elements. Like people.Select(p => <div>{p.FullName}</div>);
In Blazor, you can do it with something like this: <div>
@people.Select(p => @<div>@p.FullName</div>)
</div>
Rendering-wise, it's better to use a @foreach [0], to avoid creating too many rendering fragments, but it works.[0]: https://learn.microsoft.com/en-us/aspnet/core/blazor/compone...
var thePeople = people.Select(p => <div>{p.FullName}</div>);
var tranformed = thePeople.Select(d => d.Children.First()).DoSomethingElse();
The more I use blazor, the more I realise it's just old-school Webforms with a succinct pseudo-C#-syntax. it's very limiting compared to JSX1 - all logic needs to be written in 1 statement. That forces you to use nested ternaries, nullish operators, and array fns where simple workflow controls would read much better.
2 - it’s designed to render html but conflicts with JS and TS, so you can’t have a class attribute, and weird escape hatches around generic fns.
Because it's a thin wrapper directly translatable into a JS function call. `className` is JS
React opted for backwards compatibility at the time it came out, and never made the upgrade - you can write `class` in Preact.
I think they opted for what these properties are actually called in Javascript: https://developer.mozilla.org/en-US/docs/Web/API/Element/cla...
I wish they'd stick with that for all properties though.
Given that its taken the world by fire and nearly everyone adopted JSX/TSX as the templating language, doesn't seem like developer experience suffered.
> Like useMemo, that now is compiled in automatically
A completely different beast, and this one definitely makes DX suffer.
However angular, svelte, vue and more have a specific DSL for templating.
// this
<Box>
{
shouldShowAnswer(user)
? <Answer value={false}>no</Answer>
: <Box.Comment>
Text Content
</Box.Comment>
}
</Box>
// ...can be written like this
const answer = (
<Answer value={false}>
no
</Answer>
);
const comment = (
<Box.Comment>
Text Content
</Box.Comment>
);
const result = shouldShowAnswer(user) ? answer : comment;
<Box>
{result}
</Box>
In React at least, null won't render anything.#2 is a known trade-off, and so marginal and well known that it's almost not worth mentioning it.
Ofcourse you can split every control flow into separate component and combine them together finally, but that means exactly that - that every minute condition or iteration is deferred elsewhere. And it makes the actual render result hard to understand.
> every interpolation needs to be a statement
Interpolation??? JSX extends the PrimaryExpression in the ECMAScript grammar. So yeah, every JSX element/fragment is an expression, not a statement. You can't seriously consider an ECMAScript expression a limitation?
> you can split control flow into separate component[s]
You do realize that none of the variables in the example above are components, do you? They are the result of JSX elements, and if we're talking React they'd be React elements.
> combine them together [...] makes the actual render result hard to understand
So, for you, a long ECMAScript expression is a limiting factor, but composing shorter, simpler expressions into a longer, more complex one is hard to understand. Even though in your comment above you wrote:
> simple workflow controls would read much better
What's your alternative?
Your'e missing the point arguing about semantics of components or JSX elements, the point is that if you want to avoid control flow via expressions, you must destruct you template (however simple it may be) into tiny template fragments that makes it harder to understand the complete rendered picture.
Imaging if JSX could look like this:
<div class="container">
My list
@for(item in list) {
<h2>{item.name}</h2>
@if(item.loading){
<p>Loading...</p>
} @else if (item.error) {
<p>Error loading item</p>
} @else {
<p>{item.description}</p>
}
}
</div>
This is much easier than the suggested JSX approach: const renderItem = (item) => {
if(item.loading){
return <p>Loading...</p>
} else if (item.error) {
return <p>Error loading item</p>
}
return <p>{item.description}</p>
}
const renderList = (list) => {
return list.map(item => {
return <>
<h2>{item.name}</h2>
{renderItem(item)}
</>
})
}
return <div class="container">
My list
{renderList(list)}
</div> <div class="container">
My list
{
list.map((item) => {
let content = null;
if (item.loading) {
content = <p>Loading...</p>;
} else if (item.error) {
content = <p>Error loading item</p>;
} else {
content = <p>{item.description}</p>;
}
return (
<>
<h2>{item.name}</h2>
{content}
</>
);
})
}
</div>
VS <div class="container">
My list
@for(item in list) {
<h2>{item.name}</h2>
@if(item.loading){
<p>Loading...</p>
} @else if (item.error) {
<p>Error loading item</p>
} @else {
<p>{item.description}</p>
}
}
</div>
I still don't see any limitation.JSX eventually is not a templating engine, it’s just sugar over createComponent/createElement functions, and thats its downside, we are creating html components that need templates to render.
But in the end this is a small syntactical difference (how it looks). You could have a custom code viewer that does these translations for you.
From what I see in https://github.com/expo/exact/blob/main/contract/syntax/src/... and https://github.com/expo/exact/blob/main/contract/syntax/src/... - it's unclear this will scale to the levels of complexity that arbitrary UIs demand.