Passing an object with a custom toString() as a 'class' attribute
See original GitHub issueI am writing a library using objects with a custom toString()
method to render class names. Simplified example:
const object = {
toString() {
return computeClassNames();
},
// ... other data
};
element.className = object;
It works great on plain DOM and React, but can’t work with Preact because if a class attribute is an object, it will use the object enumerable keys associated with a truthy value to format the classname.
I understand why you chose to do this, and I find it quite useful. But would you consider changing the condition to format the class objects to something like:
if (lastSimple && lastSimple.toString === Object.prototype.toString) {
attributes.class = hashToClassName(lastSimple);
}
or (might be more robust)
if (lastSimple && String(lastSimple) === '[object Object]') {
attributes.class = hashToClassName(lastSimple);
}
or (not the cleanest but might be more efficient)
if (lastSimple && Object.prototype.toString.call(lastSimple) === '[object Object]') {
attributes.class = hashToClassName(lastSimple);
}
I could change my library to return an object with keys as class names, but I’d prefer not to add code specific to preact.
Thank you
Issue Analytics
- State:
- Created 7 years ago
- Comments:14 (6 by maintainers)
Top Results From Across the Web
Java: custom toString to fit any class - Stack Overflow
I'm working on a project that has a package full of classes with a lot of big toString()s that print the attributes and...
Read more >Object.prototype.toString() - JavaScript - MDN Web Docs
The toString() method returns a string representing the object. This method is meant to be overridden by derived objects for custom type ...
Read more >Object toString() Method in Java - GeeksforGeeks
Custom attributes been passed as in arguments ... we have to override the toString() method of the Object class in our Best_Friend class....
Read more >Object.ToString Method (System) - Microsoft Learn
Returns a string that represents the current object.
Read more >Android: Passing an arbitrary object to a custom View
Define an interface BitmapCacheProvider with a single method provideBitmapCache() ; · Make your Activity class implement the interface defined in ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Absolutely, should be seeing this land very soon. Cheers!
We can definitely remove the object check, that’ll save bytes and it’s actually totally superficial - it was added to avoid setting
<div foo="[object Object]">
, but doing so isn’t really an issue.The
function
check is to prevent this, so we’ll likely want to keep it: