Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

246
Views
What exactly does it mean that a component unmounts in React? Does it disappear after it's unmounted?

let say I have a functional Home component:

const Home = () => {
  console.log('outside useEffect')
  useEffect(() => {
    console.log('inside useEffect')
    
    // cleanup function
    return () => { console.log('unmounted') }
  }, [])

  return <div>Hello Word</div>
}

The output in the console shows:

outside useEffect
inside useEffect
unmounted
inside useEffect

My questions are:

  1. When does the component unmount, does it happen automatically after it has rendered once or when?
  2. What exactly does happen when the component unmounts? Does the component get deleted/removed? If yes then how am I still able to see the Hello World even if it unmounts.
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Normally a component gets unmounted when for example it's rendered conditionally and the conditional become false. DummyComponent below gets unmounted every time state become false. And when a component is unmounted, it's removed from the DOM, and therefore you won't see it in the page.

const DummyComponent = () => {
  useEffect(() => {
    console.log("mounted");
    return () => {
      console.log("unmounted");
    };
  }, []);
  return <div>Hello Word</div>;
};

const App = () => {
  const [state, setState] = useState(true);
  return (
    <div>
      <button onClick={() => setState(!state)}>Toggle Dummy Component</button>
      {state ? <DummyComponent /> : <></>}
    </div>
  );
};

What you have right now is introduced by React v18 where each component gets mounted and immediately unmounted and mounted again, when you are in strict development mode. I explained the reason why in this QA on Stack Overflow. And it's so quick that you are not noticing. The production mode is as before, explained in the first section.

about 4 years ago · Juan Pablo Isaza Report

0

In React 18 Strict Mode, each component is unmounted & remounted again.
https://reactjs.org/blog/2022/03/29/react-v18.html

Perhaps that's why "inside useEffect" is printed after "unmount".

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!