question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

setRef is possibly leaky

See original GitHub issue

Did some experimenting after reading reactjs/reactjs.org#834 and it turns out that changing a *ref prop can indeed cause a memory leak if it is handled by utils/reactHelpers#setRef. React will automatically clean up every ref attached to a node on update/unmount but every ref that is not attached to the node will still hold a reference to that node.

Live example: https://codesandbox.io/s/pkm11r8kjx

We would need to bookkeep every *ref prop that is passed to one of our components during its lifecycle and clean it up on cDU and cWU.

Not sure if this has any actual impact. Just documenting it in case someone actually encounters this issue.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
eps1loncommented, Apr 1, 2019

A fix naturally emerges by using a custom hook:

// useForkRef.js
export default function useForkRef(refA, refB) {
  /**
   * This will create a new function if the ref props change.
   * This means react will call the old forkRef with `null` and the new forkRef
   * with the ref. Cleanup naturally emerges from this behavior
   */
  return useCallback(
    refValue => {
      setRef(refA, refValue);
      setRef(refB, refValue);
    },
    [refA, refB]
  );
}
// usage.js
function Component({ buttonRef: buttonRefProp }) {
  const buttonRef = React.useRef();
  const handleRef = useForkRef(buttonRef, buttonRefProp);
 
  React.useEffect(() => buttonRef.current.focus(), []);

  return <button ref={handleRef} />
}

The codesandbox is updated.

Once every component that uses setRef is a function component we can use this hook instead and make setRef private.

0reactions
ivp-devcommented, Oct 16, 2022

I could not reproduce memory leak with functional components. There is my try - draft Everything seems to work correctly. I am intresting in can I manually assign instance of ref or it could cause problem with memory leak or somethink else.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TS2531 with useRef - reactjs - Stack Overflow
I'm getting an issue TSLink throwing TS2531 (Object is possibly 'null'.) in a code on my project to the point I'm doing the...
Read more >
useRef does not persist value when used with createRoot
When we use React.useRef with ReactDOM.render it renders child component of main app component only once. If the current behavior is a bug, ......
Read more >
Solved: useRef "Object is possibly 'null'" TypeScript Error
Find out how you can fix TypeScript errors when using the useRef React hook.
Read more >
Hooks API Reference - React
useRef returns a mutable ref object whose .current property is initialized to the passed argument ( initialValue ). The returned object will persist...
Read more >
How To Fix Memory Leak Issue In React Js Using Hook
Memory Leak Issue Is the first step toward the lower performance of the project. ... import { useCallback, useEffect, useRef, useState } from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found