Skip to content

Instantly share code, notes, and snippets.

@isthatcentered
Last active March 14, 2019 11:00
Show Gist options
  • Save isthatcentered/4ee0c2e3c9410d65c435eb53c95fadc4 to your computer and use it in GitHub Desktop.
Save isthatcentered/4ee0c2e3c9410d65c435eb53c95fadc4 to your computer and use it in GitHub Desktop.
React - Cloning children with new props / passing props to children
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