reactjs / rfcs

RFCs for changes to React

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Feature request] Allowing multiple refs on same DOM element

illuminist opened this issue · comments

Context

I'm working on an app which need to make use of 3rd-party lib. But problem came when the lib need a ref access to DOM element and I also have ref that need to assign to the same DOM element.

So it comes down to I have 2 ref objects that need to be assigns into the same element.

Proposal

Make ref prop to accept an array of ref object or call back.

Which should have a signature like

{
  ref: 
    | ((el: Element| null) => void)
    | React.RefObject<Element> 
    | (((el: Element| null) => void) | React.RefObject<Element>)[]
}

This is possible already and it is not too difficult, as you can see here.

@leodr That's what I'm doing as workaround. It should be more convenience if add the feature to react directly.

@illuminist that would be 227 extra Bytes that only a very small part of Reacts users ever needed - for something that is easily implementable in userspace. That stuff adds up pretty fast.

Just don't forget to maintain req equality on the derived ref. It might not work that great out of the box - gregberge/react-merge-refs#5

commented

Hi, thanks for your suggestion. RFCs should be submitted as pull requests, not issues. I will close this issue but feel free to resubmit in the PR format.