7

I am working with a third-party component which doesn't forward the ref to its DOM component and unfortunately, I need to get a ref to its DOM element in my code.

The code below obviously fails:

const ThirdPartyComponent = (props) => {
  return <div>I'm a third party component</div>;
};

const MyComponent = () => {
  const ref = useRef();

  return <ThirdPartyComponent ref={ref} />;
};

"Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?"

See codesandbox here.

Is there anyway to get a ref to the DOM element inside the third party component? I'm aware of the deprecated findDOMNode API. I'm not too keen on it, but even though, I did not manage to make it work.

Note: I know that what I'm asking for is not considered as good practice. The reason I'm doing this is to have react-beautiful-dnd work with DevExtreme's Reactive Grid. Draggable components expects a ref to the draggable elements (the <tr>s here, which I don't have). Just trying to simplify the question.

2
  • Try using a wrapper component Commented Apr 27, 2021 at 13:25
  • That was my initial workaround. But in the actual use case, the element is a TR, and TRs can't be wrapped in another element 😢 Commented Apr 27, 2021 at 14:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.