Nahush Farkande Nahush Farkande - 8 months ago 53
Javascript Question

Augmenting Object in React

I am working on a project where I have to attach a few utility functions to Javascript

object as follows:

function isEmpty(a, b) {...}
Object.prototype.isEmpty = isEmpty;

Now the problem i am facing is, since I am working with react, I am guessing the above code is also attaching the
function to the constructed Components. And this works fine as long as I don't use the native html tags i.e.
inside my components which is not possible. I get the following warning

Warning: Unknown prop `isEmpty` on <div> tag.
Remove these props from the element. For details, see

when I use the native html tags. Is there any way to augment the
object without getting this error in react?

Answer Source

The problem is that an object extension like this is enumerable. You need to use defineProperty

BTW: this is still a bad idea