Should I remove a reference to a DOM element when a component unmount in react?

12,865

By react documentation you need only to clean up globals elements such as invalidating timers, canceling network requests, or cleaning up any subscriptions that were created in componentDidMount().

A reference will be destroyed with the component on unmount cycle.

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

Share:
12,865
Radex
Author by

Radex

Back-end developer based in Lisbon. I love Python and C++.

Updated on June 22, 2022

Comments

  • Radex
    Radex over 1 year

    I have a react component which take a dom reference when mounting (I know this is an edge case). I would like to know if it is necessary set to null the property which host the dom. Or does react take care of it?

    componentDidMount() {
       this.elm = document.getElementById('foo')
    }
    
    componentWillUnmount(){
       this.elm = null
    }