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.

react-redux v.7 doesn't allow refs on connected components

See original GitHub issue

Do you want to request a feature or report a bug? Bug

What is the current behavior? When I updated to the latest version of react-redux, I started seeing the following warning on many of my connected components.

react-dom.development.js:506 Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?

React doesn’t allow refs on functional components, so any components that were accessed with a ref in past react-redux versions are now broken.

What is the expected behavior? My connected components that were previously exposed with refs would still be able to have a ref.

Which versions of React, ReactDOM/React Native, Redux, and React Redux are you using? Which browser and OS are affected by this issue? Did this work in previous versions of React Redux? react v.16.8.6, react-dom v.16.8.6, react-redux v.7.0.3

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:18 (9 by maintainers)

github_iconTop GitHub Comments

7reactions
markeriksoncommented, May 20, 2019

First, function components cannot have refs.

With React-Redux specifically:

  • in v5 and earlier, you have to add the withRef: true option to connect(), put a ref on the Connect(MyComponent) wrapper, and then call wrapperComponent.getWrappedInstance()
  • in v6 and v7, you have to add the forwardRef : true option to connect(), and the ref that you put on the Connect(MyComponent) wrapper will actually be the instance of MyComponent

If you are not supplying the forwardRef option in v6/7, then I would expect the ref to be applied to the internal wrapper component, which is now a function component, and thus you’re getting that error message.

So, make sure you’re passing {forwardRef : true} to connect(), and things should work okay.

2reactions
markeriksoncommented, May 20, 2019

Basically, change withRef to forwardRef, and then delete the .getWrappedInstance() part of componentRef.getWrappedInstance(). componentRef is your own component now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-redux-v6: withRef is removed. To access the wrapped ...
has been passed to connect , adding a ref to the connected wrapper component will actually return the instance of the wrapped component....
Read more >
Advanced React/Redux Techniques | How to Use Refs on ...
In this post, I'm going to walk you through how to use refs on a React Component that's connected to a Redux store...
Read more >
How to Use Refs on Connected Components | by Mehran Khan
Before react-redux 6 this is how we use it . ref={ref => this.chartsComponent = ref.getWrappedInstance()}. we had to use {withRef: true } in...
Read more >
React Hook Form: A guide with examples - LogRocket Blog
Learn all about using forms in React with React Hook Form, including how to create and validate forms — even with third-party components....
Read more >
Connect | React Redux
If a mapStateToProps function is specified, the new wrapper component will subscribe to Redux store updates. This means that any time the store...
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