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.

How to forward refs?

See original GitHub issue

Alright, so I have been trying to use and imported SVG with react-pose. React-pose has nothing to do with the problem. A simple example would be:


import ExpandSVG from './assets/expand.svg';

const ExpandIcon = React.forwardRef((props, ref) => (
  <ExpandSVG ref={ref} />
));

function Example() {
  const refExpandIcon = useRef(null);

  function handleClick() {
    console.log(refExpandIcon);
  }

  return (
    <div>
      <button
        type="button"
        onClick={handleClick}
      >
        Log ref of the Icon Below
      </button>
      <ExpandIcon ref={refExpandIcon} />
    </div>
  );
}

So when I click the button I would expect that the refExpandIcon would be anything but null. But instead I get a null;

image

Also I get the following error:

image

Removing the forwardRef does not make a difference.

Any ideas? I feel like it is a super simple fix

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

5reactions
tristanMatthiascommented, Nov 7, 2019

Can we please put this in 🙏? Happy to write a PR if needed

2reactions
Finessecommented, Jul 12, 2019

I think it’s a good idea. I wrote it in my message:

The author of this library must forward the ref, not you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

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 >
How to use React's forwardRef function - Felix Gerschau
forwardRef is a helper function from React that allows us to forward a component's ref to another one. This tutorial will teach what...
Read more >
ReactJS Forwarding Refs - GeeksforGeeks
The forwardRef method in React allows parent components to move down (or “forward”) refs to their children. ForwardRef gives a child ...
Read more >
Using forwardRef in React to clean up the DOM
React forwardRef is a method that allows parent components pass down (i.e., “forward”) refs to their children. Using forwardRef in React gives ...
Read more >
How can I use forwardRef() in React? - Stack Overflow
The only way to pass a ref to a function component is using forwardRef. When using forwardRef, you can simply pass the ref...
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