isComponentOfType function

bool isComponentOfType(
  1. ReactElement? instance,
  2. dynamic typeAlias, {
  3. bool traverseWrappers = true,
  4. bool matchParentTypes = true,
})

Returns whether instance is of the type associated with typeAlias, which can be a component's:

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;
}