Skip to content

[react-wrappers] useRef doesn’t return the current element #166

@dutchcelt

Description

@dutchcelt

When using useRef to get the current element I get always get an empty object when referencing from a React Wrapper element.
I encountered this on a project I’m working on and could replicate it with the react-wrapper demo in the cem-tools repo.

I adapted the App.tsx file to reproduce the bug. Or, what I think is a bug. Could it be a timing issue?

demo/react-app/src/App.tsx

import { useState, useRef, useEffect } from "react";
import "./App.css";
import { MyButton } from "lit-app/dist/react";

function App() {
  const [count, setCount] = useState(0);
  const myButtonRef = useRef(null); // <-- initialValue is null

  const counter = () => {
    console.log(myButtonRef.current); // <-- logs `{}`
    setCount((count) => count + 1);
  }

  useEffect(() => {
    console.log(myButtonRef.current); // <-- logs `{}`
  },[]);

  return (<MyButton ref={myButtonRef} onClick={counter}>
    count is {count}
  </MyButton>);
}

export default App;

Note: Replacing <MyButton> with <my-button> makes the issue go away and ref={myButtonRef} returns the Custom Element.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions