Skip to content

Instantly share code, notes, and snippets.

Last active April 4, 2021 17:17
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Cleaning up the effect
import React, { useEffect } from "react";
import ReactDOM from "react-dom";
const App = () => {
useEffect(() => {
console.log("component mounted");
const scrollHandler = () => {
console.log("page scrolled");
window.addEventListener("scroll", scrollHandler);
return () => {
window.removeEventListener("scroll", scrollHandler);
console.log("component unmounted");
}, []);
const unmount = () => {
ReactDOM.render(<p>Hello</p>, document.getElementById("root"));
return (
{" "}
<p>Open console and Scroll this page</p>{" "}
<button onClick={unmount}>Unmount component</button>{" "}
{" "}
Lorem Ipsum is simply dummy text of the printing and typesetting
industry. Lorem Ipsum has been the industry's standard dummy text ever
since the 1500s, when an unknown printer took a galley of type and
scrambled it to make a type specimen book. It has survived not only five
centuries, but also the leap into electronic typesetting, remaining
essentially unchanged. It was popularised in the 1960s with the release
of Letraset sheets containing Lorem Ipsum passages, and more recently
with desktop publishing software like Aldus PageMaker including versions
of Lorem Ipsum{" "}
</p>{" "}
ReactDOM.render(<App />, document.getElementById("root"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment