maslianok / react-resize-detector

A Cross-Browser, Event-based, Element Resize Detection for React
http://maslianok.github.io/react-resize-detector/
MIT License
1.25k stars 91 forks source link

Monitoring child component #107

Closed slava-lu closed 3 years ago

slava-lu commented 3 years ago

I wanted to update your HOC example with the parent component (ParentDiv):

import { withResizeDetector } from 'react-resize-detector';

const CustomComponent = ({ width, height, targetRef }) => 
<ParentDiv>
  <div ref={targetRef}>{`${width}x${height}`}</div>
</ParentDiv>

export default withResizeDetector(CustomComponent);

But it did not work for me because I still get the width of the ParentDiv, rather than the child div I attach ref to.

slava-lu commented 3 years ago

Looks like I just needed to wrap a child component in React.forwardRef

maslianok commented 3 years ago

Sorry for the late response. I don't think you were waiting for my help since you found the correct answer for your problem :)

forwardRef is the function you are searched for ))