React, removeEventListener and bind(this) gotcha
✓ Published0🌍 Public
RRestuta
Last edited Jan 13, 2016
Created on Dec 21, 2015
This example demonstrates a common memory-leak pitfall in React ES6 components: using `bind(this)` directly inside `addEventListener` and `removeEventListener` calls. The code shows a `resize` listener attached to `window` in `componentDidMount` and removed in `componentWillUnmount`, but because each `bind()` creates a new function reference, the removal fails. It then illustrates three fixes: binding once in the constructor, reassigning the method to its bound version, or using a class-level arrow function, highlighting how context binding affects cleanup in lifecycle methods.
AI-generated description