使用React.cloneElement会导致类型错误,我似乎无法解决.
class Dropdown extends React.Component<{ children?: React.ChildrenArray<React.Element<typeof Item>> }> { render() { React.Children.map(this.props.children,child => React.cloneElement(child) ); } }
以下类型错误:
91: React.cloneElement(child,{ ^^^^^ read-only array type. Inexact type is incompatible with exact type v-------------------------- 91: React.cloneElement(child,{ 92: onClick: () => this.setState({ open: false }),93: }),-^ exact type: object type
据我所知,这是将React.Children与React.cloneElement结合使用的正确方法.
解决方法
我不确定您使用的是哪个版本的流程,而且我没有< Item>的功能定义,但是当您删除?时它似乎有用?来自儿童,因此需要阵列:
//@flow import * as React from 'react' const Item = () => 'hello world' class Dropdown extends React.Component<{ children: React.ChildrenArray<React.Element<typeof Item>> }> { render() { React.Children.map(this.props.children,child => React.cloneElement(child) ); } }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。