4

I know there is normally unmounting the child in a parent and it's not okay to unmount a child itself. But I am just curious about it that why the unloading (closing the page) will not trigger the unmount event. Something interesting behind this design? I looked around but found nothing till now.

Thank you for any idea provided here!

Best wishes, Hearen.

Hearen
  • 7,420
  • 4
  • 53
  • 63

1 Answers1

6

Closing the page will actually discard the main process/thread which handles the rendering of the web page. In that case, there is actually no way of calling the unmounting method since the thread which calls that method is discarded.

Sajith Edirisinghe
  • 1,707
  • 1
  • 12
  • 18
  • Thank you. Yes, your point is right but actually I can know it by my local test. Here what I truly want to know is `why` react implement the `unmount` event in this way. Is there something special related to this design? – Hearen Nov 20 '17 at 13:43
  • 2
    In the lifecycle of a react app, multiple components will be added/removed to/from the DOM. In some cases, you might want to register event listeners in `componentDidMount()` method like `DataStore.on("update", this.updateData);` when a component is added to the DOM. But when the component is removed from the DOM it is MUST to unregister these listeners in the `componentWillUnmount()` like `DataStore.removeListener("update", this.updateData);`. Otherwise memory leak will occur in your application. This is why react has provided a `componentWillUnmount()` life cycle event. – Sajith Edirisinghe Nov 20 '17 at 14:44