Live data from Hacker News

How to Build Reactive Declarative UI in Vanilla JavaScript

jsdev.space

11–20 of 20 posts

Re: How to Build Reactive Declarative UI in Vanilla JavaScript

#11
post #2

For the life of me I don’t understand why people absolutely insist on using JavaScript to render HTML. Backend frameworks do HTmL just fine. DOM manipulations can be simplified to just a few actions: remove, Add, change. The other types of manipulations and interactive features can be sprinkles of JavaScript instead of hundreds of kilobytes of the stuff. HTMX, Hotwire/Turbo, LiveView are just so much saner to me.

> For the life of me I don’t understand why people absolutely insist on using JavaScript to render HTML. Backend frameworks do HTmL just fine.

There’s an entire universe of front-end developers who don’t even know JavaScript. React is the only thing they’ve ever touched and they’re completely helpless without it.

Re: How to Build Reactive Declarative UI in Vanilla JavaScript

#12
Have you heard of hyperapp? From the official [0][@hyperapp/html]:

    import { app } from "https://unpkg.com/hyperapp";
    import {
        main,
        h1,
        button,
        text,
    } from "https://unpkg.com/@hyperapp/html?module";

    const Subtract = (state) => ({ ...state, count: state.count - 1 });
    const Add = (state) => ({ ...state, count: state.count + 1 });

    const page = ({count}) =>
        main([
            h1(text(count)),
            button({ onclick: Subtract }, text("-")),
            button({ onclick: Add }, text("+")),
        ]);

    app({
        init: (count = 0) => ({ count }),
        view: page,
        node: document.getElementById("app"),
      })
I can't imagine building anything anymore with the overly verbose bloat that is React.

[0]: https://github.com/jorgebucaran/hyperapp/tree/main/packages/...

Re: How to Build Reactive Declarative UI in Vanilla JavaScript

#13
post #9

I'm experimenting with recreating the whole DOM tree like this: function render() { restoreFocus(() => document.body.replaceChildren(App())) } function App() { return ( createElement('div', { className: 'App' }, createElement('h1', null, 'Hello, World'))) } function createElement(tag, props, ...children) { const elem = document.createElement(tag) for (const [k, v] of Object.entries(props || {})) if (k === 'ref') v.el…

Looks an awful lot like https://github.com/jorgebucaran/hyperapp

Re: How to Build Reactive Declarative UI in Vanilla JavaScript

#14
post #9

I'm experimenting with recreating the whole DOM tree like this: function render() { restoreFocus(() => document.body.replaceChildren(App())) } function App() { return ( createElement('div', { className: 'App' }, createElement('h1', null, 'Hello, World'))) } function createElement(tag, props, ...children) { const elem = document.createElement(tag) for (const [k, v] of Object.entries(props || {})) if (k === 'ref') v.el…

Looks an awful lot like https://github.com/jorgebucaran/hyperapp

Both are based on the signature of React.createElement. JSX gets compiled to something like that.

https://react.dev/reference/react/createElement

Re: How to Build Reactive Declarative UI in Vanilla JavaScript

#16

Have you heard of hyperapp? From the official [0][@hyperapp/html]: import { app } from "https://unpkg.com/hyperapp"; import { main, h1, button, text, } from "https://unpkg.com/@hyperapp/html?module"; const Subtract = (state) => ({ ...state, count: state.count - 1 }); const Add = (state) => ({ ...state, count: state.count + 1 }); const page = ({count}) => main([ h1(text(count)), button({ onclick: Subtract }, text("-")…

this is great! and looks like https://mithril.js.org

Re: How to Build Reactive Declarative UI in Vanilla JavaScript

#17
The real problem vdom and more complex frameworks solve for me is dealing with much more complex state i.e. lists.

When dealing with lists there are so many possible ways of updating them (full updates, insertion/removal at an index, update at an index, ...) that manually mounting and unmounting single items by hand gets unbearable. You must then do some kind of diffing at the framework level to get good performance and readable code.

I would like to see "VanillaJS" articles talk both more and more in depth about this problem.

Re: How to Build Reactive Declarative UI in Vanilla JavaScript

#19
post #6

This is a really weird website, I glanced over a bunch of different articles and all read like AI slop to me. Indeed, a detecting tool like GPT Zero is "highly confident" that 97% of this article is AI generated, while AI Detector returns "We are 100% confident that the text scanned is AI-generated". Curious if this is an uncanny valley situation, because there aren't that many tells (dashes, etc.) in the text itself…

I don’t think the article does justice to the declarative programming approach. The title feels more like clickbait for programmers.

Re: How to Build Reactive Declarative UI in Vanilla JavaScript

#20
post #11
post #2

For the life of me I don’t understand why people absolutely insist on using JavaScript to render HTML. Backend frameworks do HTmL just fine. DOM manipulations can be simplified to just a few actions: remove, Add, change. The other types of manipulations and interactive features can be sprinkles of JavaScript instead of hundreds of kilobytes of the stuff. HTMX, Hotwire/Turbo, LiveView are just so much saner to me.

> For the life of me I don’t understand why people absolutely insist on using JavaScript to render HTML. Backend frameworks do HTmL just fine. There’s an entire universe of front-end developers who don’t even know JavaScript. React is the only thing they’ve ever touched and they’re completely helpless without it.

You can't write React without Javascript. Even the most basic React demos require you to write JS, if only to increment a counter.

Perhaps they don't really "know" the entire monstrosity of Javascript, but that's a tall order. JS is such a big language, with so many redundant features, that most developers will use only a fraction of it.

Post reply on HN