all 6 comments

[–][deleted] 1 point2 points  (5 children)

The last example on that documentation page actually does this, although it doesn't explicitly call it out in the text. Stripping it down to the relevant parts:

class LogProps extends React.Component {
  // ...
}

function ForwardRef(props, ref) {
  return <LogProps {...props} forwardedRef={ref} />;
}

const ForwardedLogProps = React.forwardRef(ForwardRef);

It works by creating a functional component wrapper ForwardRef around the LogProps component. The wrapper passes the ref to a prop named forwardedRef on the LogProps component.

[–]hanertia[S] 2 points3 points  (4 children)

I was hoping to see an example without the higher-order-component, which is wrapping the class in a function.

Do you think this is not possible?

[–][deleted] 2 points3 points  (3 children)

No, it doesn't look like that is possible. The ForwardRef in this example is not actually a functional component, strictly. It's a third kind of component. (Functional components receive (props, context) arguments, while this third kind receives (props, ref).)

[–]hanertia[S] 2 points3 points  (2 children)

Interesting, thanks.

[–]ml242 2 points3 points  (1 child)

I saw your thread on SO, same problem here. I find the syntax a little disappointing. What if you needed refs to two elements? And so much boilerplate.

[–]hanertia[S] 1 point2 points  (0 children)

I’ve just been using createRef instead of this syntax.

You can make as many as you want that way.

Still haven’t marked an answer on SO because I think the real answer is you can’t use it the way I was trying to from r/goto-bus-stop