使用React.cloneElement会导致类型错误,我似乎无法解决. class Dropdown extends React.Component{ children?: React.ChildrenArrayReact.Elementtypeof Item } { render() { React.Children.map(this.props.children, child = React.cloneEl
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="http://img.558idc.com/uploadfile/allimg/210615/1919552A0-0.jpg"></script> <script src="http://img.558idc.com/uploadfile/allimg/210615/1919556443-1.jpg"></script>
SO编辑器似乎不喜欢Flow,所以尝试“尝试流程”,这里:
https://flow.org/try/#0PTACDMBsHsHcCgCWBbADtATgFwAQCocBDAZxwCUBTQgY13A2mRwHIMrbn55roA7Y3AEksFJgF4cACgCUOMQD4WACwqQYOWJkgATTt0glSAEQaptcXjgoAPEb22lKNLADoAwo3S8KvLAB4AbxxqJUQdNl4ALnJ2VzdQ8J8AQQwMQgBPPydaFwBRSFEffyx01ApocBxhUXlFAF9FAPgcFpaI7QoMGRwm1r6W7LiE7QiXZEJUSSxQ4hdUU1mQsJGfABpg4blFZv7dmOcXahhvfMLfSSWdaR296QBuG5a6+DqgA