我試圖將道具傳遞給我的子組件,但是我有這個錯誤:標記上的未知道具'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