React Js unmounts components that are not in use. When React unmounts components, it executes
Below is how to implement
import { Component } from "react";
import { Link } from "react-router-dom";
export default class App extends Component{
componentWillUnmount() {
console.log("Unmounting App")
}
render(){
return <Link to="/toFunctionalComponent">To Functional Component</Link>
}
}
I can navigate another page by clicking on the
Now I land on the FunctionalComponent page. Here is how to implement
import { useEffect } from "react"
import { Link } from "react-router-dom"
export const FunctionalComponent = () =>{
useEffect(() => {
return(() => {
console.log("Unmounting FunctionalComponent")
})
},[])
return(
<Link to="/">To Home</Link>
)
}
I use a regular
My components may start services like timers, subscribe to services, or make network requests upon mounting. Those services are not necessary after the component unmounts. Leaving them as they are may cause slowness and poor performance in the application. Therefore, I can use the