Hi Guillermo! Thanks for the question.
The short answer is yes, if you are using the custom type key approach that the article is demonstrating.
The reason is because the type filtering is done at runtime and TypeScript types are compile-time tools.
However, if you are not able to add a custom type key prop to the component that you want to find, probably because you don’t own that component, you can still find and filter by using the react-nanny
package.
In this case, you will need to pass in the module:
import { getChildByType } from 'react-nanny';
import MyComponent from './MyComponent';getChildByType(children, MyComponent);
Notice how MyComponent
that we are passing in as an argument is NOT a string? You can do this and it will find the first child that matches the type of MyComponent
.
All of the react-nanny
functions that match by type are flexible enough to accept a module as well as a string type to match an intrinsict element type or a custom component with that value as the custom type key.
I hope this helps!