by queuep · 6y ago · 0 comments
Looks like som obfuscated JS-code..
Personally, I think indentation w/ hyperscript tends to look more natural for large sets of props, e.g.
h('div', {
id: 'hello',
title: 'hello'
onclick: () => {
console.log('hello')
},
}, [
// hi
'hello'
])
// vs
<div
id="hello"
title="hello"
onclick={() => {
console.log('hello')
}}
>
{/*hi*/}
hello
</div>
With Mithril's variety of hyperscript in particular there are also some other neat features, such as the ability to pass css selectors as the first argument, e.g. `h('input[type=password].password')` or `const TailwindComponent = '.rounded-lg.md:w-56'; const element = <TailwindComponent />`.Personally, I prefer Hyperscript. It took maybe a day for me to get over its perceived ugliness and actually appreciate that it's plain, valid JavaScript.
There are some bad templates implementation, and some good ones. The original PHP and ASP are bad. But that doesn't mean templates are bad.
<cf_menu position="left">
<cf_menu_item current="true" label="Home" url="/"/>
<cf_menu_item label="Accounts" url="/accounts" />
</cf_menu>https://github.com/seancoyne/awesome-coldfusion#application-...
I’ve heard this reaction before when people see hyperscript-like APIs. I don’t understand it. Isn’t it abundantly clear what is going on if you know DOM and JS? What is your suggestion if you don’t like hyperscript and don’t like JSX? Parsing string templates at run-time?
If you keep your components small, as is best practice, whatever gut reaction one feels to it (which IMO is misplaced to begin with) is minimized, and quite readable.
Also, don't forget that `h` is just a function, so nothing prevents you from creating your convenience functions, which allow more concise/readable code, eg:
const div = (...args) => h("div", ...args) const span = (...args) => h("span", ...args)
(or something along those lines)
I've experimented with this approach before (in ClojureScript) and you can get pretty far with it:
https://github.com/enumatech/cljs-abi-viewer/blob/state-chan...
In fact the Hoplon (https://hoplon.io/) framework as already doing this many years ago, even just using the DOM directly.
I agree with another commenter that a lot of the common objections to JSX and React more generally, especially on HN, sound like things that people who've never used it but only seen a few examples would say.
It's a shame, because with a little more experience those people could complain about stuff like how the complex form logic story in React just isn't really there yet, or how you can't really provide a lot of the subtle touches that go into making a really polished form UX without having to wrap an uncontrolled component inside a controlled one and handle all the state interactions manually.
Forms are boring, I know, and everybody hates it when they have to be complicated. But they exist and are used for a reason, and I wish someone with fewer deadlines than I have right now would take the time to really nail down some of this stuff.
const define = (componentDefinition) => (props, ...children) => isReactNode(props) ? React.createElement(componentDefinition, {}, props, ...children) : React.createElement(componentDefinition, props, ...children);
const elements = new Proxy({}, {get: (target, tagName, _) => define(tagName)});
// ...
const {div, h1, button, input} = elements;
But although I really disliked JSX at first, once I tried it a little, I realised that it's about making the code describe what you want rather than how to get it. That mind set shift is actually valuable, even if underneath it's still turning into function calls.If you have to do that anyway, I think I could use JSX instead of "pure" JS.
Plus, once you've done it, Typescript is easy to slot in, and that by itself makes the effort worthwhile.
The most ergonomic solution (well, atleast for me) has been their old factory API upon which I layered a set of convenience utilities [1]. I have been trying it out in a hobby project and I find the reduction of className boilerplate, elimination of closing tags etc. quite productive. And unlike alternatives like react-pug etc. I don't have to compromise with type checking of attributes.
I'll probably release it as a library after doing some performance evaluation and if needed, wrapping it in a babel-macro [2] that eliminates the runtime overhead.
--
[1] Example in CodeSandbox: https://codesandbox.io/s/pedantic-shape-lq9q9?file=/src/reac...
Code in github gist: https://gist.github.com/lorefnon/53377e4d6a6b13adbcfa155f486...
view: () => div({id: "app", class: "container"}, [
div({class: "filter"}, [
" Filter: ",
span({class: "filter-word"}, "ocean"),
button({}, "\u270E")
]),
I'm admittedly used to this from years using Elm but to my eye I'd far rather use this than JSX.A good approach is to show us what we need to do with no other dependencies (i.e. just as you're doing now with this h() function), and link to alternative syntaxes/libraries that may be optionally used instead.
Idiomatic JavaScript alternatives that don't involve compilation include:
- https://github.com/zaceno/hyperlit, and the aforementioned - https://www.npmjs.com/package/@hyperapp/html
but please keep the current didactic of piecemeal introduction of concepts, providing clear reasons for their existence!