connect() does not work with React.forwardRef
See original GitHub issueSpecifically, 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:
- Created 5 years ago
- Reactions:32
- Comments:61 (28 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
We chatted with @sebmarkbage and he pointed out that if you want to “redirect” ref to some inner component,
forwardRef
needs to be outside ofconnect
, not inside it.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 byconnect()
itself because it would need to passref={forwardedRef}
to the wrapped instance (which isn’t possible from the user code).I was confused on what that exactly means, so I can spare you the research: use
{ forwardRef: true }
as option toconnect