Describe the bug
When a functional component is wrapped with a proxy that has an undefined prototype, executing the render method throws a TypeError:
Cannot read properties of undefined (reading 'render')
To Reproduce
- Create a functional component
- Wrap it with something that has the
prototpe property set to undefined
- Render it using
render
One way to easily achieve this is to wrap the functional component with a Sinon spy:
import {JSDOM} from "jsdom";
import {spy} from "sinon";
globalThis.document = new JSDOM().window.document;
const spiedComponent = spy(() => {
return 'foo';
});
render(createElement(spiedComponent , {}), document.body); // TypeError*
Expected behavior
The functional component is rendered.
The culprit
The following piece of code wrongly assumes that, if an object has the prototype property, the said prototype attribute is not undefined. This is not true, per ECMAScript specification:
|
'prototype' in newType && newType.prototype.render; |
I can confirm that, be replacing 'prototype' in newType && newType.prototype.render; with 'prototype' in newType && newType.prototype?.render;, the error goes away. Actually, would the source code be written with a typed language like TypeScript, it would probably not compile.