Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of component with lifecycle hooks
// How to have render props that bind `this` context to functions that need them
// (note: not arrow functions)
// Example illustrates hooks at all stages
export class LifeCycle extends Component {
constructor(){
super()
}
shouldComponentUpdate(){
// return false to skip render
return this.props.shouldComponentUpdate && this.props.shouldComponentUpdate.call(this)
}
componentWillReceiveProps(){
this.props.componentWillReceiveProps && this.props.componentWillReceiveProps.call(this)
}
componentWillUpdate(){
this.props.componentWillUpdate && this.props.componentWillUpdate.call(this)
}
componentDidUpdate(){
this.props.componentDidUpdate && this.props.componentDidUpdate.call(this)
}
componentWillMount(){
this.props.componentWillMount && this.props.componentWillMount.call(this)
}
componentDidMount(){
this.props.componentDidMount && this.props.componentDidMount.call(this)
}
render(){
return ((typeof this.props.render).toLowerCase() === 'function')
? this.props.render.call(this)
: this.props.render
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment