Skip to content

Instantly share code, notes, and snippets.

@zehan12
Created September 20, 2024 08:37
Show Gist options
  • Select an option

  • Save zehan12/fc6e2f1644c209966a02784be90db117 to your computer and use it in GitHub Desktop.

Select an option

Save zehan12/fc6e2f1644c209966a02784be90db117 to your computer and use it in GitHub Desktop.
import React, { useEffect } from "react";
function MyComponent() {
useEffect(() => {
// Set up some side effect here, such as an event listener
window.addEventListener("resize", handleResize);
// Cleanup when the component unmounts
return () => {
window.removeEventListener("resize", handleResize);
console.log("Component unmounted");
};
}, []); // Empty array ensures this runs only once
const handleResize = () => {
console.log("Window resized");
};
return <div>MyComponent is mounted!</div>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment