Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: useEffect hook, dom ref is null in unmount callback. #20875

Closed
itutto opened this issue Feb 24, 2021 · 2 comments
Closed

Bug: useEffect hook, dom ref is null in unmount callback. #20875

itutto opened this issue Feb 24, 2021 · 2 comments
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@itutto
Copy link

itutto commented Feb 24, 2021

DOM element references created with useRef are not null-ed by the time useEffect cleanup callback is called.

React version:

17
The same code works as expected in React below v17

Steps To Reproduce

  1. Create a functional component with a simple div returned and referenced with a useRef hook.
  2. Add a useEffect hook and try to access the dom element on unmount.

note:
I understand that a local variable is the ideal solution for storing references inside useEffect, however this behaviour of null-ing DOM element references by the time the unmount phase runs is new to React 17.

Link to code example:
https://codesandbox.io/s/interesting-water-n8c48?file=/src/App.js

The current behavior

Inside useEffect cleanup: The ref.current value is null

The expected behavior

Inside useEffect cleanup: The ref.current value should still point to the DOM element as the component has not unmounted yet.

@itutto itutto added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Feb 24, 2021
@vkurchatkin
Copy link

This is documented here: https://reactjs.org/blog/2020/08/10/react-v17-rc.html#effect-cleanup-timing

@itutto
Copy link
Author

itutto commented Feb 24, 2021

This is documented here: https://reactjs.org/blog/2020/08/10/react-v17-rc.html#effect-cleanup-timing

All clear, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

2 participants