useDimensions icon indicating copy to clipboard operation
useDimensions copied to clipboard

Change how refs are set

Open james2406 opened this issue 6 years ago • 3 comments

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.

james2406 avatar Jul 19 '19 08:07 james2406

I agree that scrolling should be a separate concern. Related to #11

curran avatar Jul 19 '19 09:07 curran

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)

wmertens avatar Aug 27 '19 07:08 wmertens

this is not hacky, it allows users more freedom to provide the ref than to rely on this hook to provide the ref.

KutnerUri avatar Aug 25 '20 20:08 KutnerUri