useDimensions icon indicating copy to clipboard operation
useDimensions copied to clipboard

Optionally allow ref to be passed in

Open third774 opened this issue 6 years ago • 2 comments

Figured this would be a good enhancement. Thoughts?

third774 avatar Mar 13 '19 19:03 third774

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).

danielkcz avatar Mar 15 '19 08:03 danielkcz

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

multimeric avatar Jul 28 '19 14:07 multimeric