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.

Unable to forwardRef

See original GitHub issue

The proper way of passing ref in React is this ref={ref} but Cleave supports only the older approach htmlRef={r => ref=r}.

But this is not sufficient for forwarding refs (docs)

The workaround I am doing is this: htmlRef={r => ref && (innerRef.current = r)} but that’s not right and it’s throwing lint error because current should be read-only

The desired state is to be able do this htmlRef={innerRef}

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:20
  • Comments:5

github_iconTop GitHub Comments

5reactions
bigwoof91commented, May 23, 2021

Would be great to have a suggested/working solution for this - running into the same issue with forwarding refs and useRef.

0reactions
bel0vcommented, Jun 13, 2022

A solution that worked for me:

// reduced example
const MyInput = React.forwardRef<HTMLInputElement, MyInputProps>((props, ref) => {
  return (
    <Cleave
      {...props}
      htmlRef={(inputNode: HTMLInputElement) => {
        if (!ref) {
          return
        }
        if (typeof ref === 'function') {
          ref(inputNode)
        } else {
          ref.current = inputNode
        }
      }}
    />
  )
})
MyInput.displayName = 'MyInput'

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to get `forwardRef` utility method to document properly
I'm using forwardRefWithAs (a fn defined in @reach/utils) in a component library and using react-docgen-typescript with Storybook to ...
Read more >
Unable to style component using forwardRef - Stack Overflow
The reason the style is disappearing is that you've defined your Test component inside your App component. That means that every time App ......
Read more >
Forwarding Refs - React
Ref forwarding is an opt-in feature that lets some components take a ref they receive, and pass it further down (in other words,...
Read more >
error warning: function components cannot be given refs ...
Iget error 'Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?' . How...
Read more >
Using forwardRef in React to clean up the DOM
In this tutorial, we'll review the concept of forwarding refs in React and how it helps us manage interactions with the DOM.
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