Skip to content

Instantly share code, notes, and snippets.

@pavel-lens
Created April 19, 2017 16:18
Show Gist options
  • Save pavel-lens/1a0d1c94df21e6b6c67cf3feb3477cb6 to your computer and use it in GitHub Desktop.
Save pavel-lens/1a0d1c94df21e6b6c67cf3feb3477cb6 to your computer and use it in GitHub Desktop.
renderWrappedChildren(children) {
// Traverse through all children with pretty functional way :-)
return React.Children.map(children, (child) => {
// This is support for non-node elements (eg. pure text), they have no props
if (!child.props) {
return child
}
// If current component has additional children, traverse through them as well!
if (child.props.children) {
// You have to override also children here
return React.cloneElement(child, {
children: this.renderWrappedChildren(child.props.children),
onChange: this.createHandleChange(child.props.onChange),
})
}
// Return new component with overridden `onChange` callback
return React.cloneElement(child, {
onChange: this.createHandleChange(child.props.onChange),
})
},
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment