useDimensions
useDimensions copied to clipboard
Change how refs are set
Hey, thanks for creating such a lightweight/performant library! I think this library could improve on its api slightly to make it easier to use in an application using refs. Instead of returning a ref as the first return value, it would be more flexible if it could be passed in as the first argument to useDimensions:
const containerRef = useRef(null);
const dimensions = useDimensions(containerRef);
This would make it easier to work with other libraries, such as useScrolling:
const containerRef = useRef(null);
const dimensions = useDimensions(containerRef);
const scrolling = useScrolling(containerRef);
I would be more than happy to make a PR for this change, however, I understand if you wouldn’t want to introduce this change as it would be breaking.
I agree that scrolling should be a separate concern. Related to #11
Sadly it would mean having to call useRef anyway and ignoring the result because of the rules of hooks. I can't think of a way around that, but I do agree it could be useful.
(Actually, in this case it would be safe to assume that we'd always get the ref and therefore would never call the useRef, but that's a bit hacky)
this is not hacky, it allows users more freedom to provide the ref than to rely on this hook to provide the ref.