developit / htm

Hyperscript Tagged Markup: JSX alternative using standard tagged templates, with compiler support.
Apache License 2.0
8.67k stars 170 forks source link

same object returned at multiple points in tree #194

Closed WesleyAC closed 3 years ago

WesleyAC commented 3 years ago

It is possible for htm to return the same object at multiple points in the tree. I found this to be very unexpected — I would think this is a bug, but if not, this should be mentioned in the readme.

import htm from "/htm.js";

function h(type, props, ...children) {
  return { type, props, children };
}

const html = htm.bind(h);

let out = html`
        <div>
        ${[1, 2].map((e) => html`
                <div>
                        <input type="text" value="foo"/>
                        <input type="text" value="${e}"/>
                </div>
        `)}
        </div>`;

console.log(out.children[0][0].children[0] == out.children[0][1].children[0]) // -> true
WesleyAC commented 3 years ago

Ah, looks like this is intentional, per #158 — there should definitely be a warning about this in the readme, this was very surprising to me, and not the behaviour I wanted. I'll look at switching to htm/mini, which seems more like what I want.