bryantheastronaut / mernCommentBox

A Tutorial on creating a MERN Stack App from the Facebook Comment React Tutorial
https://medium.com/@bryantheastronaut/react-getting-started-the-mern-stack-tutorial-feat-es6-de1a2886be50
248 stars 92 forks source link

added method to prevent infinite errors on unmount #14

Closed LongStoryMedia closed 6 years ago

LongStoryMedia commented 6 years ago

I used this tutorial as an aspect of another project, and found that error messages occur every 2 seconds when the CommentBox is unmounted because it will continue to make server requests every 2 seconds on a unmounted component. this was easy to resolve by clearing the interval in the componentWillUnmount method.

componentWillUnmount() { this.setInterval && clearInterval(this.setInterval); this.setInterval = false; }

bryantheastronaut commented 6 years ago

This is great! I appreciate you coming back and taking the time to expand this out for others! When I originally wrote this a year or so ago, I didn't really expect it to be used as much as it has been (which is amazing!), so I probably wasn't as thorough on some things as I could have been. I really should go through and clean up the medium post and readme and add bits about this in.

Anyways, I left one comment on the PR that I'd like to hear your thoughts on, then will get it merged in!

Thanks again!

LongStoryMedia commented 6 years ago

Yeah, so that is much cleaner. Your right that it wasn't needed in my situation, but that project never got too complex and didn't have any other polling going on. It's possible that you wouldn't need to set it as it's own property even with a larger project, but I think it's more efficient anyway. I haven't tested in a more complex project, but I intend too, so I'm glad to have one less thing to debug.

bryantheastronaut commented 6 years ago

Yeah I agree. No cases where you would start polling in a component and want to keep that going after you trash the component comes to mind, but that definitely doesn't mean it couldn't happen, but seems like it may be a problem of not having the polling in the right component to begin with. Thanks for the PR! Merging now.