mellis481
mellis481

Reputation: 4168

How to determine if element is of a certain component type?

I'm attempting to use the ReactIs library to determine if an element is of a certain type of component. Here is my code:

React.Children.forEach(children, child => {
  if (ReactIs.typeOf(child) === ReactIs.typeOf(MyComponent)) {
      // Do something
  }
});

This does not work as expected, however. It never makes it in my IF block.

How can I determine if an element is of a certain component type?

Upvotes: 1

Views: 752

Answers (1)

sadrzadehsina
sadrzadehsina

Reputation: 1349

It seems that the right way for checking whether a react component is a specific type or not is the below one.

React.Children.forEach(children, child => {
  if (child.type === MyComponent) {
      // Do something
  }
});

You missed a closing ) too, at the end of your if statement.

if (ReactIs.typeOf(child) === ReactIs.typeOf(MyComponent)) {
  ...
}

Upvotes: 0

Related Questions