Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kylpo
Created June 21, 2017 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylpo/4ab5563a2a2e953baae0dce497489706 to your computer and use it in GitHub Desktop.
Save kylpo/4ab5563a2a2e953baae0dce497489706 to your computer and use it in GitHub Desktop.
class Cloning_ extends React.Component {
constructor() {
super()
console.log('Cloning Constructor')
}
componentDidMount() {
console.log('Cloning Mount')
}
render() {
console.log('Cloning Render')
const { children, ...propsToPass } = this.props
const child = React.Children.only(children)
return React.cloneElement(child, propsToPass)
}
}
class Div extends React.Component {
constructor() {
super()
console.log('Div Constructor')
}
componentDidMount() {
console.log('Div Mount')
}
render() {
console.log('Div Render')
return (
<div>{this.props.children}</div>
)
}
}
function createElement(label, component, props, children) {
console.log(`${label} element`)
return React.createElement(
component,
props,
children
)
}
export default class App extends React.Component {
render() {
/*
* <Cloning_ >
* <Div/>
* </Cloning_>
*/
return (
createElement(
'Cloning_',
Cloning_,
{hi: 'hi'},
createElement(
'Div',
Div,
null,
),
)
)
}
}
// elsewhere...
ReactDOM.render(
<App />,
document.getElementById('root')
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment