This is my code
import { useEffect } from "react";
import { Link } from "react-router-dom";
export const PageOne = () => {
useEffect(() => {
return () => {console.log("PageOne")}
}, []);
return (
<>
<h1>PageOne</h1>
<Link to="/two">Page two</Link>
</>
);
}
This is what I see in the console when visit PageOne
PageOne
This is what gets printed in the console when I navigate from PageOne to PageTwo (PageOne and PageTwo have the same structure)
PageOne
PageTwo
So I think that the return function defined in useEffect runs when the component is mounted and unmounted. I am using react-router-dom and Vite in this project.
Is this normal? And, does there exist a way to run a side-effect function only when a component will be unmounted?
Sorry if I made some mistakes. I am a new developer and an English student.