0

In this Stack Overflow question:

Comparing two components - is Component X an instance of Component A

Dan Abramov explains the correct way of determining whether a child variable is an instance of a Foo React class. Because return <Foo /> doesn't actually return an instance of Foo, you can't simply use instanceof; instead you have to do:

child.type === Foo.type

That's great, except that I'm using Dan's own library, Redux, and Redux instructs you to wrap your (store-using) components with it's connect function:

connect((a) => a)(
    class Foo extends React.Component { //...
)

Unfortunately, doing that changes the .type of Foo elements to the connect function itself:

const x = <Foo />;
console.log(x.type); // connect

So, my question is, is there any way I can figure out what React class an element came from if the React class in question used Redux's connect?

Community
  • 1
  • 1
machineghost
  • 33,529
  • 30
  • 159
  • 234

1 Answers1

1

If you will pass withRef = true in connect options, as per the documentation, you can later use x.getWrappedInstance().type to figure out what React class an element came from.

elmeister
  • 184
  • 1
  • 3
  • 6