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
Published 4 years ago
Updated 3 years ago

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.

egghead
egghead
~ 17 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today