[英]Can't pass children with props using React.cloneElement?
I got this error 我得到这个错误
invariant.js:42 Uncaught Error: Element type is invalid
doing this 做这个
const ListWrap = ({ children, onChange }) => {
return <div>{React.cloneElement(children, { onChange })}</div>
}
But no issue when I do 但是我什么时候没问题
const ListWrap = ({ children }) => {
return <div>{children}</div>
}
But I can't pass the props by merely doing return <div>{children}</div>
. 但是我不能仅仅通过
return <div>{children}</div>
来传递道具。 Any clue what went wrong? 任何线索出了什么问题?
React.cloneElement
takes a single child. React.cloneElement
带一个孩子。 This should fix it: 这应该解决它:
const ListWrap = ({ children, onChange }) => {
return (
<div>
{React.Children.map(children, child => React.cloneElement(child, { onChange }))}
</div>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.