我正在尝试在testInstance
上实施递归迭代,以查找具有给定类型的最接近的父级。
docs说:“与此测试实例相对应的组件类型。例如,Button组件具有Button的类型。”
但是当我尝试获取任何react组件的类型时,都会得到:react-component-type。父元素是Col
。
那么如何使用类型,它可以用来解决此问题?
function getNearestParent(component, targetParent) {
if (component.parent && component.parent.type === targetParent.type) {
return component.parent;
}
return getNearestParent(component.parent, targetParent);
}