Skip to content

"TypeError: Cannot read properties of undefined (reading 'render')" when wrapping a functional component with a proxy that has an undefined prototype #5011

@ericmorand

Description

@ericmorand
  • Check if updating to the latest Preact version resolves the issue (tested with V11 beta)

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions