Describe the bug
When preact is reconciling vdom to the live dom for a property that's defined on the target element, it goes through this code:
else if (
name !== 'href' &&
name !== 'list' &&
name !== 'form' &&
// Default value in browsers is `-1` and an empty string is
// cast to `0` instead
name !== 'tabIndex' &&
name !== 'download' &&
name in dom
) {
try {
dom[name] = value == null ? '' : value;
// labelled break is 1b smaller here than a return statement (sorry)
break o;
} catch (e) {}
}`
Namely, it coerces null/undefined values to the empty string before doing a property assignment. This can lead to unexpected behavior for custom element properties that are not expected to be string-valued (and for which null/undefined may be legitimate property values).
To Reproduce
Steps to reproduce the behavior:
- Go to https://codesandbox.io/s/zen-satoshi-vw6c8?file=/src/index.tsx (note the initial undefined behavior)
- Click on any of the numeric-valued buttons and see the expected results.
- Click on the null or undefined behavior and see the misleading '55' output rather than the expected initial behavior.
Note that in the initial rendered case, the "base" property of the element is undefined and the component renders accordingly. However, after selecting another value, there's no way to get back to that same initial state. Additionally, since this property was not coded to expect strings, the addition operator results in string concatenation which can produce misleading output when the intent was to specify null/undefined.
Expected behavior
It should be possible to specify null/undefined values for custom element properties.
Describe the bug
When preact is reconciling vdom to the live dom for a property that's defined on the target element, it goes through this code:
else if ( name !== 'href' && name !== 'list' && name !== 'form' && // Default value in browsers is `-1` and an empty string is // cast to `0` instead name !== 'tabIndex' && name !== 'download' && name in dom ) { try { dom[name] = value == null ? '' : value; // labelled break is 1b smaller here than a return statement (sorry) break o; } catch (e) {} }`Namely, it coerces null/undefined values to the empty string before doing a property assignment. This can lead to unexpected behavior for custom element properties that are not expected to be string-valued (and for which null/undefined may be legitimate property values).
To Reproduce
Steps to reproduce the behavior:
Note that in the initial rendered case, the "base" property of the element is undefined and the component renders accordingly. However, after selecting another value, there's no way to get back to that same initial state. Additionally, since this property was not coded to expect strings, the addition operator results in string concatenation which can produce misleading output when the intent was to specify null/undefined.
Expected behavior
It should be possible to specify null/undefined values for custom element properties.