Skip to content

Memory leak in Resizable #282

@NightRare

Description

@NightRare

Hi,

I think the following code here (in Resizable component) did not remove the event listener attached to the window, which has caused the GC not being able to clean up the data of Resizable and its children even if the components' React lifecycles have ended.

    constructor(props) {
        super(props);
        this.state = { width: 0 };
    }

    componentDidMount() {
        window.addEventListener("resize", () => this.handleResize());
        this.handleResize();
    }

    componentWillUnmount() {
        window.removeEventListener("resize", () => this.handleResize());
    }

    handleResize() {
        if (this.container) {
            this.setState({
                width: this.container.offsetWidth
            });
        }
    }

Suggested change:

    constructor(props) {
        super(props);
        this.state = { width: 0 };
        this.handleResize = this.handleResize.bind(this);
    }

    componentDidMount() {
        window.addEventListener("resize", this.handleResize);
        this.handleResize();
    }

    componentWillUnmount() {
        window.removeEventListener("resize", this.handleResize);
    }

    handleResize() {
        if (this.container) {
            this.setState({
                width: this.container.offsetWidth
            });
        }
    }

The change has been tested on my project. Haven't done a thorough test though. Hopefully this is helpful.

Cheers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions