question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

False arguments serialized as strings

See original GitHub issue

Suppose use-case for conditional applying classes:

import { render } from "preact";
import { html } from "htm/preact";

let el = document.createElement("div");
render(
  html`
    <div class="${false} ${null} ${undefined} ${"foo"}" />
  `,
  el
);

console.log(el.innerHTML);
// <div class="false null undefined foo"></div> 

Sandbox

That’s unexpected that the htm serializes false/null class values literally. That can be worked around with clsx or alike, but.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
developitcommented, Oct 16, 2019

This is not addressable, since the behaviour described would be specific to a given attribute. For example, htm explicitly cannot treat falsy (combined or otherwise) values within some attributes as empty:

html`<a aria-hidden="${false}">visible</a>`
// should render <a aria-hidden="false">visible</a>

html`<a aria-hidden=" ${false} ">visible</a>`
// should render <a aria-hidden=" false ">visible</a>

While I can see the convenience of special-casing DOMTokenList values in HTM, it’s not feasible to implement a scalable solution for doing so.

// classnames
const cx = (...a) => a.filter(Boolean).join(' ');

html`<a class=${cx(false, 'hello', null, undefined)}>visible</a>`
// renders <a class="hello">visible</a>

In terms of my own preference or recommendation, it seems like this would be nicer to address at the pragma layer. Personally I’d just co-opt classList for the purpose:

function h(tag, props, children) {
  if (props && props.classList) props.class = props.classList.filter(Boolean).join(' ');
  return { tag, props, children };
}
const html = htm.bind(h);
html`<a classList=${[false, 'hello', null, undefined]}>visible</a>`
// renders <a class="hello">visible</a>
1reaction
dycommented, Oct 9, 2019

you can’t just remove values

The problem is not false/null values, but string attributes - htm does rendering part here, meaningless for classes and possibly all string attributes in general:

html`<div attr="foo ${false}"/>`
// expected <div attr="foo" />
// actual <div attr="foo false" />
Read more comments on GitHub >

github_iconTop Results From Across the Web

boolean serializing to string error - Stack Overflow
when you write JsonDeserializer<String> , probably you say that it is going to be your class which deserializes String -s from JSON.
Read more >
Parameter Serialization - Swagger
explode (true/false) specifies whether arrays and objects should generate separate parameters for each array item or object property. OpenAPI serialization ...
Read more >
All serialized values are strings, should be integers/booleans ...
Problem/Motivation JSON produced by the HAL and JSON normalizers provides all data values as strings. This makes it difficult for strongly ...
Read more >
JSON.stringify() - JavaScript - MDN Web Docs
The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or ...
Read more >
Fields — marshmallow 3.19.0 documentation
A double as an IEEE-754 double precision string. Function ([serialize, deserialize]) ... falsy – Values that will (de)serialize to False .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found