Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

not working with React.forwardRef #33

Open
MaxmaxmaximusGitHub opened this issue Apr 26, 2020 · 1 comment
Open

not working with React.forwardRef #33

MaxmaxmaximusGitHub opened this issue Apr 26, 2020 · 1 comment

Comments

@MaxmaxmaximusGitHub
Copy link

not working with React.forwardRef

@caasi
Copy link

caasi commented May 30, 2022

You can create an inner ref when the forwarded ref is null and coerce it to exclude callback refs.

import { ForwardedRef, RefObject, useRef } from 'react'

function useEnsuredRef<T>(ref: ForwardedRef<T>): RefObject<T> {
  const defaultRef = useRef<T>(null)
  if (typeof ref === 'function') {
    throw new Error('callback refs are not supported')
  }
  return ref === null ? defaultRef : ref
}

export default useEnsuredRef

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants