Allow passing in 'ref' to the useResizeDetector hook
See original GitHub issueHi @maslianok!
I’m faced with a conundrum: I need to access the ref
inside the onResize
callback.
But… I don’t have the ref
yet when I’m defining the onResize
, because it is the hook creates it 😕.
const useRegistry = (blockId: string) => {
const onResize = useCallback(() => {
resizeObservable.next({blockId, ref})
// ^^^ oops, no ref yet!
}, [blockId])
const {ref} = useResizeDetector({onResize})
// ^^^ too late
return {ref}
}
A solution would be to allow passing in your own ref
instead of creating it inside the hook. This way the ref
could also be reused by other custom hooks that are used before useResizeDetector
is called, like so:
const useRegistry = (blockId: string) => {
const ref = useRef()
const onResize = useCallback(() => {
resizeObservable.next({blockId, ref})
}, [blockId, ref])
useResizeDetector({onResize, ref})
return {ref}
}
I’m willing to make a PR if this solution is fine by you. Thanks 🙇.
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
I want to use useRef() to access an element in a reat-leaflet ...
The React way to do this is to nest your flyTo button inside the map container and access map with useMap() hook:
Read more >react-resize-detector - npm
1. React hook (new in v6.0.0) ; import { useResizeDetector } ; } from 'react-resize-detector' ;; const CustomComponent ; = () ; {...
Read more >Passing Refs to a Parent Component Using useRef in React.js
Hey guys! This is a short tutorial on how we can handle passing refs from a child component to a parent component.
Read more >How To Use Refs In React With Hooks
In this article I am going to go over everything you need to know about refs in order to help you never make...
Read more >react-resize-detector: Versions - Openbase
Rewritten in TypeScript; React hook support useResizeDetector (example) ... Also, we added ability to pass targetRef to avoid findDOMNode method usage (more ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks, merged!
I’ll deploy changes in the next version
Sold 😃
tbh, I don’t see any difference between these approaches. Just 2 lines instead of the 1-line condition.
Can you make a PR?