state
can be modified using this.setState()
.❗️NOTE:
this.setState()
call is asynchronous. It means you won't see the updated value of state just after thesetState
call.
Before we get into Lifecycle methods, we first need to understand what does mounting
& unmounting
means?
setup
and cleanup
. Simply, mounting
: adding nodes to the DOM, unmounting
: removing them from the DOMrender();
// If you don’t initialize state and you don’t bind methods,
// you don’t need to implement a constructor for your React component.
constructor(props) {
// you should call super(props) before any other statement.
// Otherwise, this.props will be undefined in the constructor, which can lead to bugs.
super(props);
// Don't call this.setState() here!
this.state = { counter: 0 };
// `this` binding. Alternate option is to use es6 arrow functions, to avoid this.
this.handleClick = this.handleClick.bind(this);
}
componentDidMount()
componentDidMount()
is invoked immediately after a component is mounted (inserted into the tree).❗️NOTE: Keep the last point in mind, we will make use of it in upcoming section.
componentDidUpdate(prevProps, prevState)
componentDidUpdate()
is invoked immediately after updating occurs. This method is not called for the initial render.componentWillUnmount()
componentWillUnmount()
is invoked immediately before a component is unmounted and destroyed. Perform any necessary cleanup in this method, such as invalidating timers, canceling network requests, or cleaning up any subscriptions that were created in componentDidMount()
.setState()
in componentWillUnmount()
because the component will never be re-rendered. Once a component instance is unmounted, it will never be mounted again.💡 TLDR: Every
state
change will leads to an re-render of the application to reflect that particular change.