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.

connect() does not work with React.forwardRef

See original GitHub issue

Specifically, if I compose connect with another HOC that uses forwardRef, the result of forwardRef (an object) is passed as the WrappedComponent, which throws an error claiming it should only be a function.

Example

const MyComponent = React.forwardRef(() => null);
connect()(MyComponent);

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:32
  • Comments:61 (28 by maintainers)

github_iconTop GitHub Comments

123reactions
gaearoncommented, Mar 30, 2018

We chatted with @sebmarkbage and he pointed out that if you want to “redirect” ref to some inner component, forwardRef needs to be outside of connect, not inside it.

class MyComponent extends React.Component {
  render() {
    return <SomeInnerComponent ref={this.props.myForwardedRef} />;
  }
}

const ConnectedMyComponent = connect(
  mapStateToProps
)(MyComponent);

export default forwardRef((props, ref) =>
  <ConnectedMyComponent {...props} myForwardedRef={ref} />
);

There is also another scenario in which you might want to point connect()ed component’s ref to the wrapped component’s own instance (MyComponent in this example). Arguably this is what most React Redux users would want. You can’t do this in userland. It would need to be done by connect() itself because it would need to pass ref={forwardedRef} to the wrapped instance (which isn’t possible from the user code).

49reactions
JLarkycommented, Feb 2, 2019

I was confused on what that exactly means, so I can spare you the research: use { forwardRef: true } as option to connect

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using React forwardRef with Redux connect
I tried export default connect(mapStateToProps, null, null, { forwardRef: true })(MyComponent) , but it didn't work. The issue is that it can't ......
Read more >
Forwarding Refs
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 >
next link function components cannot be given refs. ...
Wrapping the functional component with react.forwardRef() is a workaround, but ideally this would be fixed upstream from consumers of this library. Open side ......
Read more >
How to use React's forwardRef function
React's references don't always behave like you would expect them to. In this tutorial, you'll learn why and how you can use the...
Read more >
React useRef, forwardRef and Some Problems you May ...
setState() in class component. And now, let's check an example with the most common usage of useRef on practice: Imagine that you have...
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