Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"getWrappedInstance is not a function" #1053

Closed
davidc360 opened this issue Oct 18, 2018 · 6 comments
Closed

"getWrappedInstance is not a function" #1053

davidc360 opened this issue Oct 18, 2018 · 6 comments

Comments

@davidc360
Copy link

Greetings,

I want to use Ref on my connect components. A search through google finds everyone suggesting to pass {withRef: true} as the 4th argument to connect() and then use ref. getWrappedInstance() to access the wrapped instance of the component.

However, when I try it I'm getting "getWrappedInstance is not a function". I read through the docs, is getRef only available in advancedConnect() now?

I attempted to use advancedConnect but the documentation for selectorFactory is difficult to understand and I couldn't make the change from connect to advancedConnect.

Here is my code snippet:

export default connect(mapStateToProps, mapDispatchToProps, null, { withRef: true })(Element);

and ref.getWrappedInstance() simple gives me "getWrappedInstance is not a function".

@timdorr
Copy link
Member

timdorr commented Oct 19, 2018

getWrappedInstance is a function on the element itself. You can use it like our tests do.

@timdorr timdorr closed this as completed Oct 19, 2018
@Venryx
Copy link

Venryx commented Mar 4, 2019

The test you referenced expects an error to occur with the given line:

<Decorated ref={comp => comp && comp.getWrappedInstance()}/>

The test says why: "To access the wrapped instance, you need to specify { withRef: true } in the options argument of the connect() call"

However, in the newest version of react-redux, the withRef option is no longer valid. When I try to use it, I get the same error expected in this test: https://github.com/reduxjs/react-redux/blob/master/test/components/connect.spec.js#L2576

So if withRef is required to use getWrappedInstance, but I get an error saying "withRef is removed" when I try to do so, how do I call getWrappedInstance?

@Venryx
Copy link

Venryx commented Mar 4, 2019

Nevermind, I don't know how I missed it, but it's just been replaced by the forwardRef option seen here (which removes the need for getWrappedInstance): https://react-redux.js.org/api/connect#forwardref-boolean

@archansel
Copy link

I got this error recently, it was not a problem before. I use version 5.0.5 and never downgrade or update. Implement it using withRef options. The weird part is, it's only happening in local, when I push to my dev server, it's working just fine

@MasonChinkin
Copy link

I got this error recently, it was not a problem before. I use version 5.0.5 and never downgrade or update. Implement it using withRef options. The weird part is, it's only happening in local, when I push to my dev server, it's working just fine

@archansel Did you ever figure this out? I just installed redux v4 and react-redux v5 into an existing project and all the getWrappedInstances broke 😢

@archansel
Copy link

Component

const withConnect = connect(
  mapStateToProps,
  mapDispatchToProps,
  null,
  { withRef: true },
);

export default compose(withConnect)(LocationModal);

Usage

setLocationModalRef = ref => {
    if (ref) this.locationModal = ref.getWrappedInstance();
  };

<LocationModal ref={this.setLocationModalRef} />

Redux "react-redux": "5.0.5"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants