Thursday, 10 June 2021

How to use componentWillUnmount with Functional Components in React

How to manage componentWillUnmount with useEffect

If you add a return function inside the useEffect function, it is triggered when a component unmounts from the DOM. This looks like:


import React, { useEffect } from 'react';

const ComponentExample => () => {

    useEffect(() => {

        return () => {

            // Anything in here is fired on component unmount.

        }

    }, [])