React.js cheatsheet
devhints.io
devhints.io
spread operator shorthand for passing component props.
Suppose: const now = moment()
Then, you can pass it down to a component like this:
<TimeInput {...{now}} />
Which is equivalent to
<TimeInput now={now} />
With many props, it becomes easier on the eyes and differentiates from passing event handlers e.g. <TimeInput {...{now}} myHandler={this.handleKeyPress} />
return <some pseudo-HTML-code>;
always makes my eyes bleed. var someXML = <text>Why not?</text>;
Sadly, it's no longer supported in Firefox, but there are some ECMAScript variants still supporting E4X syntax, probably most notably ActionScript 3 as used in Adobe Flash.Personally I love JSX, since it gives you all of the same power as template-based languages with virtually turing complete DSLs, but with JavaScript instead of a DSL. Not always as elegant, but almost always easier to understand.
My only real complaint is what JSX compiles down to. I think that it should either compile down to some data (like JSON data) or JSX should just be an alternative function call syntax. Instead, it compiles down to calls to React in most implementations (though usually, like with TypeScript for example, you can customize what it calls exactly.) The calls also vary depending on if the tag is lowercase or uppercase, since that's how React tells primitive tags apart from components. Frankly, a little ugly.
React.createElement(
"div",
null,
React.createElement(
"div",
null,
React.createElement(
"p",
null,
React.createElement(
"a",
{ href: "foo" },
"Link"
)
)
)
);
document.getElementById('root')
); div([
div([
p([
a({ href: "foo" }, "Link")
])
])
])
[1] https://reactjs.org/docs/react-without-jsx.htmlI guess react-hyperscript is what I was looking for, but I've grown to quite like JSX.
import {createElement as h} from 'react'
import {render} from 'react-dom'
render(
h('div', null,
h('div', null,
h('p', null,
h('a',
{href:'foo'},
'Link',
),
),
),
),
document.getElementById('root')
)It uses template strings instead of requiring a parser, turning your example into something more like:
return htm`<Some pseudoHTMLcode />`;
https://github.com/developit/htm