Skip to content

How to pass getBoundingClientRect to a child component #2841

Answered by atomiks
joelstein asked this question in Q&A
Discussion options

You must be logged in to vote

You can add a prop called getPositionReference for usePopover that uses a layout effect to call refs.setPositionReference().

https://codesandbox.io/p/sandbox/vigorous-shirley-yxzx9m?file=%2Fsrc%2FPopover.tsx%3A64%2C1-70%2C44

<Popover getPositionReference={getPositionReference}>
// You'd probably want to use a `useEffectEvent` utility here instead
// to prevent requiring the function to be memoized/static reference.
React.useLayoutEffect(() => {
  if (getPositionReference) {
    context.refs.setPositionReference(getPositionReference());
  }
}, [context.refs, getPositionReference]);

If using useEffectEvent (not in stable React yet, can be polyfilled, which it is in this library internally):

c…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by joelstein
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants