Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use ref on custom element? #113

Open
quintesse opened this issue Jul 24, 2018 · 5 comments
Open

How to use ref on custom element? #113

quintesse opened this issue Jul 24, 2018 · 5 comments

Comments

@quintesse
Copy link

First, sorry for using the issue tracker for a question, but I didn't see any mention of a mailing list or forum!

So my question is how to use <Measure> with a custom element, like for example a semantic-ui control? I'm trying to do this:

<Measure ....>
  {({measureRef}) => (<Card ref={measureRef} ....> .... </Card>)}
</Measure>

but that results in "TypeError: Failed to execute 'observe' on 'ResizeObserver': parameter 1 is not of type 'Element'".

I can add a <div> around it and put the ref on that but that results in the bounds not being exactly correct (they tend to be quite a bit bigger than the actual element I'm trying to measure).

Any ideas?

@adekbadek
Copy link

You need to pass ref to the element rendered by the custom component, for example:

const Card = ({ innerRef, children }) => <div ref={innerRef}>{children}</div>

more on that here

@quintesse
Copy link
Author

quintesse commented Jul 30, 2018

I probably didn't explain this well enough, but what I wanted to say was:

So my question is how to use <Measure> with a custom element not made by me and therefore unable to change the way it works, like for example a semantic-ui control?

So there's no way for me to pass an innerRef.

@adekbadek
Copy link

I guess if the library you're using does not provide a way of passing a ref to then rendered element, then you're out of luck :/

But maybe I'm wrong and there is some (maybe hacky) way of accessing the ref?

@quintesse
Copy link
Author

quintesse commented Jul 30, 2018

Well yes, there is, I'm currently using ReactDOM.findDOMNode() to get the underlying DOM element and just ask for it's bounds directly using plain DOM + JS. It means not using >Measure> anymore, I made my own <DOMRef> component that has a callback that gives me the DOM node. I know they advise against using ReactDOM.findDOMNode() but I don't see any other way in my case.

@cloudlena
Copy link

cloudlena commented Oct 17, 2018

You can just wrap the custom element with a div. In most cases, the dimensions of the wrapper should be the same as the ones of the inner element:

<Measure ...>
  {({measureRef}) => <div ref={measureRef}><Card ...> ... </Card></div>}
</Measure>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants