setRef is possibly leaky
See original GitHub issueDid 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:
- Created 5 years ago
- Comments:5 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
A fix naturally emerges by using a custom hook:
The codesandbox is updated.
Once every component that uses
setRef
is a function component we can use this hook instead and makesetRef
private.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.