questions about setElementProp()
See original GitHub issueI’ve been studying the source code closely, and have a few questions regarding setElementProp()
:
-
How or why is it useful/relevant to set a single prop? Aren’t props always a set? If so, shouldn’t this be
setElementProps()
(plural) to avoid the loop and function-call overhead at every call site? -
Regarding setting of properties (not attributes), this line:
try { element[name] = null == value ? "" : value } catch (_) {}
Why the null (ish) check and defaulting to empty string? It’s not a given that the property being set is even a string property. It’s also not a given that I wouldn’t want to set a property to null. These are object-properties, but they seem to get treated like attribute-values?
And why the
catch
? Capturing a stack-trace is expensive, isn’t it? Might be cleaner to check for existence withname in element
prior to setting? -
Regarding the overall logic:
try { element[name] = null == value ? "" : value } catch (_) {} if (typeof value !== "function") { if (null == value || false === value) { element.removeAttribute(name) } else { element.setAttribute(name, value === true ? "" : value) } }
Aren’t we doing double duty here? First setting as property-value, then setting as attribute-value. If we had the property-check (above) we’d could make these mutually exclusive, so properties take precedence over attributes, and so we don’t do double assignments - probably more correct and better for performance?
For example, right now, if I do something like <div innerHTML={content}>
, I will end up with the same HTML injected as an attribute-value.
Issue Analytics
- State:
- Created 6 years ago
- Comments:10 (10 by maintainers)
I’ve opened a new issue for that discussion - I’m closing this one as there’s nothing actionable here.
What are some of those properties we are also setting as attributes, but we shouldn’t?