This:
<Component prop="value" />
directly translates to this: React.createElement(Component, {prop: "value"}, null);
You can skip JSX entirely, and use functions only. If that's too verbose for you, some people alias `React.createElement` to `h` à la Hyperscript[1] and that becomes h(Component, {prop: "value"}, null)
That's why, unlike the not-really-HTML-not-really-javascript templates of Vue and Angular, JSX is not markup. It's 99% plain old Javascript, 1% paper-thin DSL. <?php
if (condition) {
?>
<div>whatever</div>
<?php
}
?>
Yet in react everybody seems happy to do the exact same thing in javascript. if (isLoggedIn) {
return <UserGreeting />;
}
How is that different?This is fine:
<?php
if (condition) {
?>
<div>whatever</div>
<?php
{
?>
this not: if (isLoggedIn) {
return <UserGreeting />;
{ function
ThemedButton(props) {
return (
<ThemeContext.Consumer>
{theme => (
<button
{...props}
style=.
{{backgroundColor:
theme.background}}
/>
)}
</ThemeContext.Consumer>
);
}
I've been writing JavaScript for 15 years and this looks like gibberish to me. There's at least 3 context switches between JavaScript, JSX and CSS. We're abandoning separation of concerns and creating a monstrosity of programming by punctuation.Moreover, JSX components are plain old Javascript functions and classes. You write plain old code that follows normal rules if the language when you deal with them.
JSX DSL is there for convenience because it's, well, extremely convenient.
There are feeble attempts elsewhere to equate JSX with writing HTML inside PHP tags which fall apart the moment you give JSX more than a give-second cursory glance.
JSX is a paper-thin DSL on top of Javascript. JSX components are regular Javascript functions and classes. You work with them the same way you work with any function it class in JS. Unlike PHP where it was actual HTML interspersed with PHP code.
Whether I type `return (<button>foo</button)` in react or echo '<button>foo</button>' in php, it's the same thing from my point of view as an application builder.
Such as? This is the point in dispute, so you need to actually make specific arguments if you think they apply.