question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow passing in 'ref' to the useResizeDetector hook

See original GitHub issue

Hi @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:closed
  • Created 3 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
maslianokcommented, Feb 4, 2021

Thanks, merged!

I’ll deploy changes in the next version

1reaction
maslianokcommented, Feb 4, 2021

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found