When does a component unmount?

前端 未结 4 536
感动是毒
感动是毒 2020-12-25 09:52

My current understanding is that a component mounts onto the DOM when it is needed to be seen or when the route requires that component. It will also render its child compon

4条回答
  •  情话喂你
    2020-12-25 10:30

    During the VirtualDOM Reconciliation if a component existed but no longer will, the component is considered unmounted and given a chance to clean up (via componentWillUnmount).

    The reverse is true, during the reconciliation, if a component didn't exist, but now does, the component is considered ready to mount, and given a chance to prep itself (constructor / componentWillMount)

    When tearing down a tree, old DOM nodes are destroyed. Component instances receive componentWillUnmount(). When building up a new tree, new DOM nodes are inserted into the DOM. Component instances receive componentWillMount() and then componentDidMount(). Any state associated with the old tree is lost.

    https://facebook.github.io/react/docs/reconciliation.html

    That particular page is well worth a read if you haven't already. It also explains why key is pretty important for repeated elements.

提交回复
热议问题