Skip to content

Instantly share code, notes, and snippets.

@ademilter
Last active April 30, 2020 22:13
Show Gist options
  • Save ademilter/1cd5d5b9c7dfb3695f352ab3450259b3 to your computer and use it in GitHub Desktop.
Save ademilter/1cd5d5b9c7dfb3695f352ab3450259b3 to your computer and use it in GitHub Desktop.
react component children map clone props
import React from 'react'
import PropTypes from 'prop-types'
const Comp = ({ children }) => {
const childs = React.Children.map(children, child =>
React.cloneElement(child, { ...child.props, moreProps: 123 })
)
return <div>{childs}</div>
}
Comp.Header = ({ children, propA }) => {
return (
<header>
<h5>{children}</h5>
</header>
)
}
Comp.Body = ({ children, propB }) => {
return <div>{children}</div>
}
export default Comp
import React from 'react'
import Comp from './components/comp'
const Index = () => (
<Comp>
<Comp.Header propA="a">
Long <span>title</span>
</Comp.Header>
<Comp.Body propB="b">
<p>lorem ipsum...</p>
</Comp.Body>
</Comp>
)
export default Index
@ademilter
Copy link
Author

thanks @f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment