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

Expose interface for `useResizeDetector()`'s return value #171

Closed roydukkey closed 3 years ago

roydukkey commented 3 years ago

Could we get an interface for this return? This would be useful when storing the result, instead of destructuring it.

https://github.com/maslianok/react-resize-detector/blob/6d7c8a5923b1165773038c7994eed9657b58a825/src/useResizeDetector.ts#L76

maslianok commented 3 years ago

I'm not sure I get the idea. Can you maybe provide a more detailed explanation or code snippet? Or maybe create a pull request on this?

roydukkey commented 3 years ago

I'll create a PR. For instance:

import { useResizeDetector } from 'react-resize-detector';
import type { UseResizeDetectorReturn } from 'react-resize-detector';

let resizeDetector: UseResizeDetectorReturn<HTMLElement>;

export default function SingletonComponent(): JSX.Element {
    resizeDetector = useResizeDetector<HTMLElement>();
    // ...
}

export function useComponentSize () {
    // Do work with `resizeDetector`
}

Currently, this is how it has to be done. But, this involves poking around the internals of the library more so than the suggested changes.

let resizeDetector: { ref: MutableRefObject<HTMLElement | null>; height?: number; width?: number; };
maslianok commented 3 years ago

Thank you! Merged and published 6.7.6