Sign in
Log inSign up

Where should I move calls to document.addEventListener in React components?

c's photo
c
·Mar 28, 2018

Looks like componentWillMount is being deprecated.

I'm using it to check for keydown events (and componentWillUnmount for removeing the event):

componentWillMount() {
    document.addEventListener("keydown", this.handleKeyDown);
}

componentWillUnmount() { 
  document.removeEventListener("keydown", this.handleKeyDown); 
}

In the blog example they move it to componentDidMount. Should I just move it there too?

Edit: fixed the typo, I was talking about componentWillMount not componentWillUnmount.

Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct