Skip to content

Instantly share code, notes, and snippets.

@lightandluck
Forked from monicao/react.md
Created September 14, 2017 17:56
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 lightandluck/3edf07a9067f9c544ac57a1c58c02427 to your computer and use it in GitHub Desktop.
Save lightandluck/3edf07a9067f9c544ac57a1c58c02427 to your computer and use it in GitHub Desktop.
React Lifecycle Cheatsheet

React Component Lifecycle

  • getInitialState
  • getDefaultProps
  • componentWillMount
  • componentDidMount
  • shouldComponentUpdate (Update only)
  • componentWillUpdate (Update only)
  • componentWillReceiveProps (Update only)
  • render
  • componentDidUpdate (Update only)
  • componentWillUnmount/constructor
  • componentDidUnmount

TIPS

getDefaultProps

The result of getDefaultProps() will be cached and used to ensure that this.props.value will have a value if it was not specified by the parent component.

componentWillMount()

Triggered before render().

componentDidMount

Called after render. Can access refs. The componentDidMount() method of child components is invoked before that of parent components. This is the place to call external libraries, use setTimeout, make ajax requests

shouldComponentUpdate(nextProps, nextState) - Update only

called when there are new props or state changes. return false to prevent a render. good for performance.

componentWillReceiveProps(nextProps) - Update only

Called before render when props change. Access to old props. It is not triggered after the component is mounted.

componentWillUpdate(nextProps, nextState)

Invoked immediately before rendering when new props or state are being received. Not called for the initial render. Cannot use setState in this method. Use componentWillReceiveProps instead. Use this as an opportunity to perform preparation before an update occurs.

render

Triggered when the state changes.

componentDidUpdate(prevState, prevProps) - Update only

Access to prevState, prevProps Use this as an opportunity to operate on the DOM when the component has been updated.

componentWillUnmount

Clean up event bindings, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment