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.

Passing an object with a custom toString() as a 'class' attribute

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
developitcommented, Dec 7, 2016

Absolutely, should be seeing this land very soon. Cheers!

2reactions
developitcommented, Apr 6, 2017

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:

screen shot 2017-04-06 at 4 16 57 pm
Read more comments on GitHub >

github_iconTop 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 >

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