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.

undefined attribute values are converted to strings

See original GitHub issue

When attribute values are undefined or null they are appended as strings ‘undefined’ or ‘null’.

For example this code:

var none // unassigned
hx`<li.item class=${none}>...</li>`

Will result in html like this:

<li class="item undefined">...</li>

Whereas virtual-hyperscript will ignore the value and not add it to the class list.

I’m not sure if this is the correct behavior, but i expected it to do the same as virtual-hyperscript. This can be achieved by the developer by supplying a custom concat function, or by ensuring that undefined values are never used, but i think this should be handled by default?

If it is a bug, I think it can be fixed in the concat function, by making undefined parameters default to empty strings (‘’). However maybe there is a better way to do it?

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:3
  • Comments:5

github_iconTop GitHub Comments

1reaction
vladimirkosmalacommented, Dec 14, 2017

I agree with this issue, it is actually implemented with Babel/JSX. Those falsy values should be forgotten (not numbers):

  • false
  • null
  • undefined
  • empty string

Let’s compare:

❌ html`<input value="${data.value ? data.value : ''}" />`
✅ html`<input value="${data.value}" />`

Declarative conditional

❌ html`<div>${data.alert ? html`<x-alert />` : ''}</div>`
✅ html`<div>${data.alert && html`<x-alert />}</div>`

Declarative switch case with three ternary operators

❌ html`<div>${data.option === 1 ? html`<x-option1 />` : (data.option === 2 ? html`<x-option2 />` : html`<x-option3 />`)}</div>`
✅ html`<div>
${data.option === 1 && html`<x-option1 />}
${data.option === 2 && html`<x-option2 />}
${data.option === 3 && html`<x-option3 />}
</div>`

The idea is to keep it declarative (no if/else/swtich) and lisible as possible.

It could create a breaking change so v3 is a possibility. An other one is to provide this as an option. What do you think?

0reactions
Ramblurrcommented, Sep 24, 2017

I can confirm it still exists.

const imgsrc = undefined;
const img = hx`<img src=${imgsrc}/>`

This causes chrome to make http requests to /undefined

Read more comments on GitHub >

github_iconTop Results From Across the Web

Convert object values to string when values are: undefined ...
Now, I want to use Object.values(obj) so I will get an array of values. But false, undefined & null return as an empty...
Read more >
undefined - JavaScript - MDN Web Docs - Mozilla
The global undefined property represents the primitive value undefined . It is one of JavaScript's primitive types.
Read more >
Functions - AWS IoT Core
String, Decimal . The result is the absolute value of the argument. If the string cannot be converted, the result is Undefined ....
Read more >
5 Ways to Convert a Value to String in JavaScript
As you can see, the String() method handles the null and undefined quite well. No errors are thrown - unless that's what you...
Read more >
Properties - Lit.dev
Use the default converter · For Strings, when the attribute is defined, set the property to the attribute value. For Numbers, when the...
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