Skip to content

Instantly share code, notes, and snippets.

@pragyandas
Created September 12, 2017 09:58
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 pragyandas/28f842295e5641a5b0146404a345a44e to your computer and use it in GitHub Desktop.
Save pragyandas/28f842295e5641a5b0146404a345a44e to your computer and use it in GitHub Desktop.
React Component Life-cycle

componentWillMount

  • App configuration in the root component
  • Can call setState - but calling setState on a non-rendered component doesn't make sense

componentDidMount

  • Ajax calls
  • setState can be called here

componentWillReceiveProps

  • Perhaps the Ajax call initiated by the parent's componentDidMount got resolved and a stream of new properties arrived at the component
  • Here we have access to component nextProps and currentProps
  • Do auxiliary stuff from the new property
  • setState can be called here

shouldComponentUpdate

  • get access to next prop and next state
  • compare it with current prop and state to return boolean
  • do not call setState here

componentWillUpdate

  • Not used that much
  • No access to next props
  • Use componentWillReceiveProps

componentDidUpdate

  • Updating the DOM in response to prop and state changes
  • setState can be called

componentWillUnmount

  • final cleanup
  • setState can't be called
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment