我要过滤组件键值。我为此使用filter
。但我得到了错误:
TS2339: Property 'filter' does not exist on type 'string | number | boolean | {} | ReactElement<any> | ReactNodeArray | ReactPortal'.
Property 'filter' does not exist on type 'string'.
键值是div的键值。
我试图将其用作函数或常量,但仍然遇到相同的错误。
getComponent(key){
return this.props.children!.filter(component => {
return component.key === key;
});
}
我希望过滤组件的键值。也许有人遇到同样的问题?
我正在使用React-typescript
答案 0 :(得分:0)
如错误所示,为func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 0 {
return isExpanded ? UITableView.automaticDimension : 0
}
return UITableView.automaticDimension
}
列出的字符串,数字,布尔值和其他各种类型都没有称为this.props.children
的函数。因此,您首先需要确保要处理数组,然后使用类型断言来告诉TypeScript:
filter
答案 1 :(得分:0)
几周前,我遇到了类似的问题,我使用React.Children
遍历传递给组件的嵌套子项。
这是我编写的代码:
public renderByKey(childElement, key) {
return React.Children.map(childElement, child => {
if (child != null) {
if (child.props != undefined) {
if (typeof (child.type) !== 'string') {
if (child.props.key != undefined && child.props.key === key) {
return child;
}
}
if (child.props.children != undefined) {
return this.renderByKey(child.props.children,key);
}
}
}
});
}
我知道递归性带来的不便,但是我将它与其他道具(不是以钥匙为例)一起使用,并且对我来说非常完美。