componentWillUnmount method

  1. @override
  2. @mustCallSuper
void componentWillUnmount()
inherited

ReactJS lifecycle method that is invoked immediately before a Component is unmounted from the DOM.

Perform any necessary cleanup in this method, such as invalidating timers or cleaning up any DOM Elements that were created in componentDidMount.

See: reactjs.org/docs/react-component.html#unmounting-componentwillunmount

Implementation

@override
@mustCallSuper
void componentWillUnmount() {
  super.componentWillUnmount();

  // Ensure that unmounted components don't batch render
  shouldBatchRedraw = false;

  // Cancel all store subscriptions.
  _subscriptions
    // This can be null in unsound null safety when consumers are using mocked stores.
    // and haven't stubbed `listen`.
    //
    // For sound null safety, we can't work around these errors, and consumers will need to stub
    // `listen` on the mock.
    ..forEach((subscription) => _castAsNullable(subscription)?.cancel())
    ..clear();
}