How to do it...

Let's go over the life of a component, in order from the time a component is created and placed into the DOM, during its life when it may be updated, up to the moment the component is removed from the DOM. We are going to hit only the main methods, and even so it's likely that you won't get to use all of them:

  • constructor()This method is called before the component is mounted for basic setup and initialization. This method is used for all kinds of initialization. The only key rule is that you should always start by calling super(props) before doing anything else, so this.props will be created and accessible.
  • componentDidMount()This method is called after the component is mounted.
  • shouldComponentUpdate(nextProps, nextState)This method is used by React to decide whether a component needs to be re-rendered or not.
  • render()This (mandatory) method produces HTML elements, ideally based only on this.props and this.state. If the function returns a boolean or null value, nothing will be rendered. The method should be pure, not attempting to modify the component's state (which can lead to nasty loops) or to use anything but state and props.
  • forceUpdate()This method is not really a life cycle one, and you can call it whenever you want to force a re-rendering to be done.
  • componentDidUpdate(previousProps, previousState)This method is called after a component has been updated. 
  • componentWillUnmount()This method is called just before a component is going to be unmounted. 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.202.167