Skip to content

Instantly share code, notes, and snippets.

@b17z
Forked from dxlbnl/sample.js
Created August 26, 2019 16:30
Show Gist options
  • Save b17z/c7dbc676d17ac7c7de6bbfb523a79b39 to your computer and use it in GitHub Desktop.
Save b17z/c7dbc676d17ac7c7de6bbfb523a79b39 to your computer and use it in GitHub Desktop.
React component Lifecycle methods
class Component extends React.Component {
static getDerivedStateFromProps (nextProps, prevState) {
// -> nextState
// | Called after instantiating and when it receives new props
}
render () {
// -> jsx
}
componentDidMount () {
// | Called just after mounting, handy for calling the network and setting up subscriptions
// | setState will trigger rendering before the DOM is updated
}
shouldComponentUpdate(nextProps, nextState) {
// -> shouldUpdate
// | Checks whether the component should update.
}
getSnapshotsBeforeUpdate () {
// Invoked before rendering output to the DOM
// Used to mitigate any sideeffects caused by an update
// Result will be passed to `componentDidUpdate`
}
componentDidUpdate (prevProps, prevState, snapshot) {
// Invoked after updateing
// Not called for initial render
}
componentWillUnmount () {
// Called before the component unmounts
// Remove timers, subscriptions, cancel network calls, etc.
}
componentDidCatch (error, info) {
// Error boundaries for the component.
// Handy for catching errors during rendering, in lifecycle methods.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment