useDimensions
useDimensions copied to clipboard
Optionally allow ref to be passed in
Figured this would be a good enhancement. Thoughts?
It won't do any harm for sure :) Perhaps it would even make sense to switch around resulting tuple. If ref is passed, I would rather be doing [size] = useDimensions(ref) directly instead of awkward [, size] = useDimensions(ref).
I think this feature is still needed, but unfortunately this PR won't work with the new useCallback()-based internals. Since refs are just functions anyway, I'm wondering if we can do something like this:
function useDimensions({
liveMeasure = true,
boundsType = BoundsType.client,
ref = null
}: UseDimensionsArgs = {}): UseDimensionsHook {
const [dimensions, setDimensions] = useState({});
const [node, setNode] = useState(null);
const localRef = useCallback(node => {
setNode(node);
ref(node);
}, []);
// Other code
return [localRef, dimensions, node];
}
Thoughts?
Edit: I realise that this would only work for callback refs. I have to think about how this would work with a classic object-based ref