Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Protect the setStates in the callback with a conditional switch that gets flipped off in componentWillUnmount.


Not clear on what you're saying, pseudocode?


In componentWillUnmount, set a var componentUnmounted true. In componentDidMount in the callback for the fetch, check for componentUnmounted before setting state for an error or whatever you want to do.


Set a variable... set a property in the component state? Or set a static variable on the class?


I use a static var on the class.

  componentDidMount() {
   if (!this.unmounted) ...
  }

  componentWillUnmount() {
   this.unmounted = true
  }


That's not a static car on the class. It's on the instance! A static class property would entail something like `Foo.unmounted = true` (a bad idea).




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: