Last active
March 14, 2019 11:00
-
-
Save isthatcentered/4ee0c2e3c9410d65c435eb53c95fadc4 to your computer and use it in GitHub Desktop.
React - Cloning children with new props / passing props to children
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
export interface PassThroughProps | |
{ | |
children: ReactElement | |
} | |
export function PassThrough( props: PassThroughProps ) | |
{ | |
const { children, ...propsToPass } = props | |
return React.Children | |
.map( children, ( child: ReactElement ) => | |
React.cloneElement( child!, propsToPass ) ) // Clone element requires a single child | |
} | |
// Use: | |
<PassThrough | |
waffles={true} | |
pancakes="indeed" | |
> | |
<ChildToInjectTo/> | |
</PassThrough> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment