Upgrade to Pro — share decks privately, control downloads, hide ads and more …

React Lifecycle methods. Gotta Catch 'Em All!

React Lifecycle methods. Gotta Catch 'Em All!

Sometimes catchup with all of this React Component lifecycles methods is hard, even for those who worked with part of them on daily basis. React v16.3 introduced some deprecations and some new ones. We will review and retrospect all of them with real-life usage samples.

updated lifecycles docs: https://reactjs.org/docs/react-component.html
about getDerivedStateFromProps and how to migrate from componentWillReceiveProps https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html
lifecycle diagram: http://projects.wojtekmaj.pl/react-lifecycle-methods-diagram

bonus, pokemons in cli: https://github.com/lazocoder/pokemon-terminal

Bogdan Plieshka

June 27, 2018
Tweet

More Decks by Bogdan Plieshka

Other Decks in Technology

Transcript

  1. class LineupWrapper extends React.Component { componentWillMount() { const { channels,

    dispatch, } = this.props; if (!channels.length) { dispatch(fetchLineup()); } } }
  2. class TeaserList extends React.Component { shouldComponentUpdate(nextProps) { const {teasers} =

    this.props; const {teasers: teasersNext} = nextProps; return ( teasersListNext.length && lastItem(teasers) !== lastItem(teasersNext) ); } }
  3. class OSD extends React.PureComponent { constructor(props) { super(props); this.state =

    { isActive: true, isMouseOver: false, }; this.onMouseOverBound = this.onMouseOver.bind(this); } }
  4. class OSD extends React.PureComponent { state = { isActive: true,

    isMouseOver: false, }; } transform-class-properties
  5. static getDerivedStateFromProps({ streamUrl: streamUrlNext, cid: cidNext, pid: pidNext, }, {

    cid, pid, streamUrl, }) { if ( (streamUrlNext && streamUrlNext !== streamUrl) || cidNext !== cid || pidNext !== pid ) { return { shouldWave: true, }; } return null; }
  6. componentDidUpdate( prevProps, {view: viewPrev}, ) { const {view} = this.state;

    const {cdaPlayer} = this.props; // handle scroll position when view changed if (isValueChanged(viewPrev, view)) { if (cdaPlayer) { this.scrollToChannelNode(cdaPlayer); } else { this.domNode.scrollIntoView(); } } }
  7. export default class ErrorWrapper extends React.Component { state = {

    error: null, errorInfo: null, }; componentDidCatch(error, info) { this.setState({ error, errorInfo: info, }); Raven.captureException(error, {extra: info}); } render() { const {error, errorInfo} = this.state; if (error) { return ( <ScreenError errorTitle={error ? error.toString() : null} errorDescription={errorInfo ? errorInfo.componentStack : null} /> ); } return this.props.children; } }