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

Pass `targetRef` as argument to `onResize` #94

Closed johnhunter closed 4 years ago

johnhunter commented 4 years ago

Now that v5.x.x provides a targetRef property it would be useful if the ref could be passed as an argument in the onResize callback.

For example:

<ReactResizeDetector
  onResize={(width, height, ref) => {
    /* do something with ref */
  }}
>
  {({ targetRef }) => <div ref={targetRef}>...</div>}
</ReactResizeDetector>
maslianok commented 4 years ago

@johnhunter you can create and pass your own ref to the ReactResizeDetector in case you need to process it somehow.

For example

const ref = createRef(); // or useRef();

<ReactResizeDetector targetRef={ref} onResize={() => /* do something with ref */}>
  {() => <div ref={ref}>...</div>}
</ReactResizeDetector>

The main idea is to handle refs inside the library and to pass them as parameters if you don't want to create your own ref (less code, fewer rules). But if you do need to work with these refs you should create your own instances and pass them inside ReactResizeDetector.

johnhunter commented 4 years ago

Great, thanks!