cityofaustin / atd-mobile-signals-work-orders

Mobile Signals Work Orders app using React UI with Knack API
https://mobile-signals.austintexas.io
Other
3 stars 1 forks source link

Address memory leak warnings from unresolved API requests #219

Closed mddilley closed 5 years ago

mddilley commented 5 years ago

Closes #208

mddilley commented 5 years ago

This PR addresses cases when API requests are called in componentDidMount() with setState() calls chained to them. The memory leak issue pops up when a user navigates away from a view before the promise resolves, and then the app attempts to setState() on an unmounted component.

To fix this up, a class field boolean, _isMounted was introduced to capture the state of the component outside of its local state. _isMounted is false on class instantiation, set to true in componentDidMount(), and then set to false again in componentWillUnmount(). This boolean is then used as a condition for setState() after the promise resolves. I read about this approach here and read some more about class fields here.