Skip to content

[Compiler Bug]: Error when calling React.createElement(..., { ref }) #31357

@jthemphill

Description

@jthemphill

What kind of issue is this?

  • React Compiler core (the JS output is incorrect, or your app works incorrectly after optimization)
  • babel-plugin-react-compiler (build issue installing or using the Babel plugin)
  • eslint-plugin-react-compiler (build issue installing or using the eslint plugin)
  • react-compiler-healthcheck (build issue installing or using the healthcheck script)

Link to repro

https://playground.react.dev/#N4Igzg9grgTgxgUxALhAMygOzgFwJYSYAEMCaAkpgBI4C2ANgKL0K0KY4AUAlEcADrEicQmBwkyRALxEoYBACUynTFHr1uAbkFEJOWMSUBDXADo4pIzgTNW7LvxBwjmAG5GwjgDR8JaIgC+WoIBIAFAA

Repro steps

Playground link

This code:

function refInHtmlElement() {
  const ref = useRef(null);
  return React.createElement("canvas", { ref });
}

Has this error:

InvalidReact: Ref values (the current property) may not be accessed during render. (https://react.dev/reference/react/useRef) (3:3)

Even though we're using a ref in the way it's meant to be used. If we rewrite it in JSX, the error goes away:

function refInHtmlElement() {
  const ref = useRef(null);
  return <canvas ref={ref} />;
}

I find this behavior surprising, because I always thought that JSX desugared down to React.createElement anyway....

How often does this bug happen?

Every time

What version of React are you using?

Playground

What version of React Compiler are you using?

Playground

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions