Hyperhtml: Always render HTML in component

Created on 14 Aug 2017  路  1Comment  路  Source: WebReflection/hyperHTML

I have a component that can take either DOM nodes or strings, and I always want to render HTML.

Should I do it like this?

function container(contents) {
  return hyperHTML.wire()`
  <div>
    ${{
      [typeof contents === 'string' ? 'html' : 'any']: contents
    }}
  </div>
  `
}

document.body.appendChild(container('<p>Hi</p>'))
document.body.appendChild(container(hyperHTML.wire()`<p>Hi</p>`))

Thank you

help wanted question

Most helpful comment

I'd simplify the approach via:

function container(contents) {
  return hyperHTML.wire()`
  <div>
    ${typeof contents === 'string' ? [contents] : contents}
  </div>`;
}

Please watch out if you use wires without a reference you're trashing the DOM/layout every single time you invoke it.

It's OK for a one-off, but it's not ideal when you have many updates.
https://viperhtml.js.org/hyperhtml/documentation/#api-1-0

>All comments

I'd simplify the approach via:

function container(contents) {
  return hyperHTML.wire()`
  <div>
    ${typeof contents === 'string' ? [contents] : contents}
  </div>`;
}

Please watch out if you use wires without a reference you're trashing the DOM/layout every single time you invoke it.

It's OK for a one-off, but it's not ideal when you have many updates.
https://viperhtml.js.org/hyperhtml/documentation/#api-1-0

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bschlenk picture bschlenk  路  3Comments

moebiusmania picture moebiusmania  路  6Comments

BentleyDavis picture BentleyDavis  路  8Comments

rektide picture rektide  路  8Comments

zaclummys picture zaclummys  路  3Comments