react-use-measure icon indicating copy to clipboard operation
react-use-measure copied to clipboard

Dependencies are needed

Open seleckis opened this issue 5 years ago • 5 comments

Would be great to pass dependencies to this hook. For example, if a div scales with css transform useMeasure does not handle these changes, you need to resize window to get an actual size.

Please check an example: https://codesandbox.io/s/nostalgic-shannon-ckeq4?file=/src/App.js

So, the solution might be, like:

const [ref, size] = useMeasure({ dependencies: [scale] });

seleckis avatar Oct 22 '20 07:10 seleckis

useMeasure(null, [scale])
useMeasure({ ... }, [scale])

or ... (not sure if that's a good idea due to typescript)

useMeasure([scale])
useMeasure({ ... }, [scale])

?

would you want to add a pr for this?

drcmda avatar Oct 22 '20 08:10 drcmda

btw there is a forceRefresh for such cases: https://codesandbox.io/s/quizzical-margulis-95c09?file=/src/App.js:284-380

const [scale, setScale] = useState(1)
const [ref, size, refresh] = useMeasure()
useEffect(() => void refresh(), [scale])

but i agree, dependencies would probably be cleaner.

drcmda avatar Oct 22 '20 08:10 drcmda

@drcmda thanks for the tip, didn't know that. Would be great to add info about refresh to docs.

seleckis avatar Oct 22 '20 08:10 seleckis

Please check PR #25. I think to pass dependencies in the Options object is much better for maintainability instead of second argument. If it is ok, I can update docs.

seleckis avatar Oct 22 '20 10:10 seleckis

If this ever revives, maybe the dependencies could be upgraded to not only support [dependency] but to also support [{dependency, delay}] since dependencies could be css transitions and you shouldn't refresh until the transition is finished. Could you a custom useTimeout instead of useEffect.

For example, css { transition: margin 500ms ease-in-out;}

I suggest a delay would be cleaner than listening for a transitionend event and adding state to provide a dependency that is triggered after the transition ends.

kenbanks-peng avatar Sep 01 '21 08:09 kenbanks-peng