使用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>