Skip to content

Instantly share code, notes, and snippets.

@mykeels
Last active October 6, 2017 14:25
Show Gist options
  • Select an option

  • Save mykeels/06d58ac363208ba58f7ff04d6973cf1a to your computer and use it in GitHub Desktop.

Select an option

Save mykeels/06d58ac363208ba58f7ff04d6973cf1a to your computer and use it in GitHub Desktop.
The ultimate explanation for React's component lifecycle event handlers from https://tylermcginnis.com/reactjs-tutorial-a-comprehensive-guide-to-building-apps-with-react/
var FriendsContainer = React.createClass({
getInitialState: function(){
alert('In getInitialState');
return {
name: 'Tyler McGinnis'
}
},
// Invoked once before first render
componentWillMount: function(){
// Calling setState here does not cause a re-render
alert('In Component Will Mount');
},
// Invoked once after the first render
componentDidMount: function(){
// You now have access to this.getDOMNode()
alert('In Component Did Mount');
},
// Invoked whenever there is a prop change
// Called BEFORE render
componentWillReceiveProps: function(nextProps){
// Not called for the initial render
// Previous props can be accessed by this.props
// Calling setState here does not trigger an additional re-render
alert('In Component Will Receive Props');
},
// Called IMMEDIATELY before a component is unmounted
componentWillUnmount: function(){},
render: function(){
return (
<div>
Hello, {this.state.name}
</div>
)
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment