我正在尝试将props传递给我的组件子级,但出现此错误:标签上有未知的props“用户”。从元素中移除这个props。
在查看文档和问题时,我想我明白给 React.cloneElement(第二个参数)的 props 必须是 DOM 识别的属性。
所以我的问题是如何将 props 传递给组件子组件并使它们在 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