Skip to content

useRender can crash during React reconciliation #4039

@aaronadamsCA

Description

@aaronadamsCA

Bug report

Current behavior

If component CountButton contains a React hook, then React will crash when isCounter changes:

{isCounter ? (
  <Button render={CountButton} />
) : (
  <Button>No counter</Button>
)}

You can work around this by rendering the element before passing it in:

{isCounter ? (
  <Button render={<CountButton />} />
) : (
  <Button>No counter</Button>
)}

Expected behavior

Neither example should crash React.

Reproducible example

https://stackblitz.com/edit/vitejs-vite-ykx9a7mz?file=src%2FApp.jsx

Base UI version

v1.1.0

Additional context

This is a pretty big footgun. It's apparently due to React optimistically merging the state of the same component at the same position in the tree.

These are the two errors we've seen in this scenario:

Minified React error #300

Rendered fewer hooks than expected. This may be caused by an accidental early return statement.
Minified React error #310

Rendered more hooks than during the previous render.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions