bradxr
1/3/2019 - 10:04 AM

30 - componentWillUnmount()

  • there is one lifecycle method which gets executed (when implemented) right before a component is removed from the DOM: componentWillUnmount()
  • see example
state = {
  showUserComponent: true
};

removeUserHandler = () => {
  this.setState({showUserComponent: false});
}

render() {
  return (
    <div>
      {this.state.showUserComponent ? <User /> : null}
      <button onClick={this.removeUserHandler}>Remove User Component</button>
    </div>
  );
}
componentWillUnmount() {
  // component is about to get removed => perform any cleanup work here!
  console.log('I\'m about to be removed!');
}