加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – 如何在Typescript React中遍历Component的Children?

发布时间:2020-12-15 20:50:07 所属栏目:百科 来源:网络整理
导读:如何在Typescript tsx中遍历React组件的子项? 以下是有效的jsx: public render() { return ( div {React.Children.map(this.props.children,x = x.props.foo)} /div );} 但是,在Typescript中,x的类型是React.ReactElement any所以我无法获得道具.我需要做
如何在Typescript tsx中遍历React组件的子项?

以下是有效的jsx:

public render() {
    return (
        <div>
            {React.Children.map(this.props.children,x => x.props.foo)}
        </div>
    );
}

但是,在Typescript中,x的类型是React.ReactElement< any>所以我无法获得道具.我需要做某种铸造吗?

However,in Typescript,the type of x is React.ReactElement so I have no access to props. Is there some kind of casting that I need to do?

是.也更喜欢断言一词.一个快速的:

React.Children.map(this.props.children,x => (x as any).props.foo)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读