My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

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.