isComponentOfType function
bool
isComponentOfType(
- ReactElement? instance,
- dynamic typeAlias, {
- bool traverseWrappers = true,
- bool matchParentTypes = true,
Returns whether instance
is of the type associated with typeAlias
, which can be a component's:
- UiFactory (Dart components only)
- UiComponent Type (Dart components only)
- ReactComponentFactoryProxy
ReactClass
component factory- String tag name (DOM components only)
Related: isValidElementOfType
Implementation
bool isComponentOfType(ReactElement? instance, dynamic typeAlias,
{bool traverseWrappers = true, bool matchParentTypes = true}) {
if (instance == null) {
return false;
}
var instanceType = instance.type as Object?;
var type = getComponentTypeFromAlias(typeAlias);
if (type == null) {
return false;
}
var instanceTypeMeta = getComponentTypeMeta(instanceType!);
// Type-check instance wrappers.
if (traverseWrappers && instanceTypeMeta.isWrapper) {
assert(isDartComponent(instance),
'Non-Dart components should not be wrappers');
final children = getProps(instance)['children'] as List?;
if (children == null || children.isEmpty) {
return false;
}
return isComponentOfType(children.first as ReactElement, type,
traverseWrappers: true, matchParentTypes: matchParentTypes);
}
// Check parent types.
if (matchParentTypes && instanceTypeMeta.parentType != null) {
return instanceType == type || getParentTypes(instanceType).contains(type);
}
return instanceType == type;
}