Skip to content

Instantly share code, notes, and snippets.

@benjasHu
Created May 15, 2018 16:53
Show Gist options
  • Save benjasHu/48120663c4a77fc7db00bcf4088ea781 to your computer and use it in GitHub Desktop.
Save benjasHu/48120663c4a77fc7db00bcf4088ea781 to your computer and use it in GitHub Desktop.
Render dynamically React children passing props
/*
You can use this function to dynamically render children in a Rect component, passing into parent's props.
It checks if the children call is a function or a React element and render it with parent's props
*/
function renderChildren( children, props ) {
return toString.call(children) == '[object Function]' ? children(props) : React.Children.map(children, child => React.cloneElement(child, props))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment