1. 61
    Clean up Location Event handler Resources when Page Unmounts in React useEffect
    25s

Clean up Location Event handler Resources when Page Unmounts in React useEffect

Colby Fayock
InstructorColby Fayock
Share this video with your friends

Social Share Links

Send Tweet

Now that we have finished up with our map app, let's do a little cleaning up! Here we are going to go through our Location Event Handler and clean it up for when the page unmounts.

Click here for exercise instructions

Colby Fayock: [0:00] Anytime we create an event listener, we want to try to be thoughtful and clean those up on the component unmounts. This way, we don't have any stray resources being hung up.

[0:07] To do this, our useEffect hook lets us return a statement to run after unmounts. We're going to tell this to return a new function, or we're going to tell our map to turn off our location found event along with that function.

[0:18] When we go to the browser and click Find My Location, we're not going to see anything different, but now we know we're going to be cleaning up those resources whenever it unmounts.