Created
September 27, 2018 11:13
-
-
Save jimmed/44c4bb9af8511b8fab4e01deab15973b to your computer and use it in GitHub Desktop.
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
import React from "react"; | |
class LifecycleDemo extends React.Component { | |
constructor() { | |
super() | |
this.handleClick = this.handleClick.bind(this); | |
} | |
handleClick() { | |
this.props.onClose(); | |
} | |
componentDidMount() { | |
console.log('componentDidMount') | |
document.addEventListener('click', this.handleClick); | |
} | |
componentWillUnmount() { | |
console.log('componentWillUnmount') | |
document.removeEventListener('click', this.handleClick) | |
} | |
render() { | |
console.log("render called"); | |
return ( | |
<div | |
style={{ position: "absolute", top: 50, left: 50, right: 50, bottom: 50, background: 'red' }} | |
> | |
Lifecycle demo component | |
</div> | |
); | |
} | |
} | |
export default LifecycleDemo; |
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
import React from 'react'; | |
import LifecycleDemo from './LifecycleDemo'; | |
class LifecycleWrapper extends React.Component { | |
constructor(){ | |
super(); | |
this.state = { | |
text: '', | |
showDemo: true | |
}; | |
this.handleChange = this.handleChange.bind(this); | |
this.handleClick = this.handleClick.bind(this); | |
} | |
handleChange(event){ | |
this.setState({ | |
text: event.target.value | |
}); | |
} | |
handleClick(){ | |
this.setState({ | |
showDemo: !this.state.showDemo | |
}); | |
} | |
render(){ | |
return ( | |
<div> | |
<input type="text" onChange={this.handleChange} /> | |
<button onClick={this.handleClick}>Show/Hide child</button> | |
{this.state.showDemo ? <LifecycleDemo text={this.state.text} onClose={this.handleClick} /> : null} | |
</div> | |
) | |
} | |
} | |
export default LifecycleWrapper; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment