我试图将道具传递给我的子组件,但是我有这个错误:标记上的未知道具'user'。从元素中删除此道具。如何将道具传递给React.cloneElement的孩子?
在查看文档和问题时,我想我明白给React.cloneElement(第二个参数)的道具必须是DOM认可的属性。
所以我的问题是如何将道具传递给子组件并使它们在this.props中可访问?
这里是我的代码:
render() {
const { children } = this.props
const { user } = this.state
const childrenWithProps = React.Children.map(children, child =>
React.cloneElement(child, { user })
)
return (
<div>
{ childrenWithProps }
</div>
)
}
编辑:孩子组件的propTypes
ChildrenPage.propTypes = {
user: PropTypes.object
}
export default ChildrenPage
你能粘贴'ChildrenPage'的声明吗? 。我是一个非常确定的错误是在ChildrenPage组件中。 – Tomasz