Last active
April 30, 2020 22:13
-
-
Save ademilter/1cd5d5b9c7dfb3695f352ab3450259b3 to your computer and use it in GitHub Desktop.
react component children map clone props
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks @f