React Component Unmounting Phase
📖 Scenario: You are building a simple React app that shows a message when a component is visible. When the component is removed from the screen, you want to run some cleanup code to log a message.
🎯 Goal: Create a React functional component that displays a message. Add a button to toggle showing or hiding this component. Use the unmounting phase to log a message when the component is removed.
📋 What You'll Learn
Create a React functional component called
Message that returns a <div> with the text 'Hello, I am here!'.Create a state variable called
showMessage in the main component to control if Message is shown.Add a button that toggles
showMessage between true and false.Use
useEffect with a cleanup function inside Message to log 'Message component is unmounting' when it is removed.💡 Why This Matters
🌍 Real World
React components often need to clean up resources like timers or subscriptions when they are removed from the screen to avoid bugs or memory leaks.
💼 Career
Understanding the unmounting phase is essential for React developers to manage component lifecycle and resource cleanup properly.
Progress0 / 4 steps