I am starting to use hyperHTML have a question
starting with
const data = [1,2,3]
Using wire
hyperHTML.bind(document.getElementById('root'))`
<h1>Hello, world!</h1>
${ data.map( num => hyperHTML.wire()`<li>${num}</li>` ) }
`;
Using string
hyperHTML.bind(document.getElementById('root'))`
<h1>Hello, world!</h1>
${ data.map( num => `<li>${num}</li>`) }
`;
Why is wire better?
When wire
has no "id" (obj,string) it will return an element without references
Here is the documentation but they dont say why one should use wire over string.
Thanks for any help
Edit:
Just thinking.. would define be better to use? Something like:
hyperHTML.define(numberListItem, num => `<li>${num}</li>`)
hyperHTML.bind(document.getElementById('root'))`
<h1>Hello, world!</h1>
${ data.map( num => ${{numberListItem: num}}) }
`;
But now you would fill up the name space of every small element :(
The main problem in your question is the example itself: a list of primitives, in this case numbers, used as generic element content.
This is not a so common real-world use case, where numbers comes from data and data is weakly referenciable.
However, if it's exactly a list of few numbers that you want inject as
LI
elements, hyperHTML lets you do that, and if that's all you need, just go for it.Now let me try to explain your question:
Why is wire better?
hyperHTML gives you a way to define various kind of content, including:
You might want/need to use hyperHTML features to create elements as one-off operation.
A form, an input, a button, an image, if you want to quickly create a DOM element, you can do that via hyperHTML because it doesn't lock you in, it's rather an opt-in abstraction/library.
That is why you could relate a specific data object to a wire, but you could also simply use wires without references: it's OK for quick prototyping or small amount of changes.
Now let me show you few examples from that (reduced) list of features.
Point 1: text
If instead of plain numbers you have a list of book titles, what will your unwired code produce?
The answer is that the second book will cause layout issues due
<HTML>
tag, while usingwire()
the title will be shown as expected.Automatically sanitized layouts are then a benefit of a one-off wire.
Point 2: attributes
Consider this case:
That is nothing you can create as string for these reasons:
Accordingly,
wire()...
is a more convenient way to create an element.Point 3: lazy content
Above example will fetch stars/rates from an end point and show them in place once resolved.
The layout will be filled up as rates get resolved.
About your Edit
The
define
method makes sense only if you represent a way to resolve the value.Using an
Array
as define key is really not the best way to go.If you need to update the same
data
over and over, you can use that data as a reference and pass keys as IDs.Keys/IDs could be the index of the data, an info however unrelated with the current list item, or something more unique, like the book title or, more generically, the data primitive content, assuming it's unique.
Your initial code would then look like the following:
The
wire()
signature is indeed to accept a reference object you'd like to relate to some layout, and also a layout type and/or its id.These are all valid calls:
With these primitives you can relate any list of information to a specific node. It's the keyed concept from React or Vue on steroids.
Map your own wire
If none of the above mechanism satisfies your requirements, you can always create your own wires and use them as you like.
In above eaxample you could even
data.sort()
and still obtain the rightLI
for the right number.Real-world use cases
I hope you agree the most common situation is that the source of information, your
data
array, is quite often, if not always, an array of objects.In this case you just wire the book info, and let everything else work as expected without trashing nodes or injecting
LI
s on the wild.I hope it's now clear why anyone, and any library or third parts project, could benefit from hyperHTML wires, regardless if the rendering bit is an hyperHTML bound node.