Skip to content

Bug: Memory Leak in React 17 when using useRef Hook #27892

@xiangpaopao

Description

@xiangpaopao

React version: 17.0.2
React-dom version: 17.0.2

Steps To Reproduce

  • Create a React component View that uses useRef to create a ref and passes it to a div element.
  • In another component App, include a toggle button that can mount and unmount the View component.

Here is the code

import React, { useState } from "react";

export default function App() {
  const [visible, setVisible] = useState(true);

  return (
    <div className="App">
      <button onClick={() => setVisible(!visible)}>toggle view</button>
      {visible ? <View /> : null}
    </div>
  );
}

const View = () => {
  const ref = React.useRef<HTMLDivElement>(null);

  return <div ref={ref}>this view is visible</div>;
};

  • Open the provided demo in the Edge browser: React Memory Leak Demo.
  • Click the "toggle" button to unmount the View component.
  • Use Edge's Detached DOM tool and click collect garbage button and then click get detached elements,.
  • You can see the div element to be detached element.image

Link to code example:
https://codesandbox.io/p/sandbox/react-memory-leak-useref-mngn43

The current behavior

The DOM element has not been garbage collected.

The expected behavior

The DOM element has been garbage collected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: UnconfirmedA potential issue that we haven't yet confirmed as a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions