Skip to content

TypeError: Cannot set properties of null (setting '__m') with preact 10.19.4 when destroying a component that calls useId #4277

@acelaya

Description

@acelaya
  • Check if updating to the latest Preact version resolves the issue

Describe the bug

We have a helper which renders components via render(<SomeJSX />, container) (where container is an HTMLElement), and then destroys them via render(null, container) to make sure side effect clean-ups are executed.

This worked fine with preact <=10.19.3, but with 10.19.4 it throws an error when render(null, container) is called, only if the rendered component is internally using useId().

To Reproduce

Create a component that calls useId(), then render it and then destroy it

import { render } from 'preact';
import { useId } from 'preact/hooks';

function App() {
  useId();
  return <div>Hello World</div>;
}

const container = document.getElementById('app');
render(<App />, container);
render(null, container);

You can see the error in action here https://stackblitz.com/edit/create-preact-starter-wih6a1?file=src%2Findex.jsx

Basically, as soon as the component has a call to useId(), it crashes when calling render(null, container).

Steps to reproduce the behavior:

  1. Go to https://stackblitz.com/edit/create-preact-starter-wih6a1?file=src%2Findex.jsx
  2. Wait until the component renders. You should see "Hello World" on the right side.
  3. Open the browser console. You'll see an error like this:
    image

If you remove the call to useId() or downgrade to preact 10.19.3, the error will not be reproducible.

Expected behavior

No error is thrown.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions