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

Add useRef usage example for detecting DOM changes #7372

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

mattcarrollcode
Copy link
Contributor

@mattcarrollcode mattcarrollcode commented Dec 17, 2024

Inspired by @chaance's Bluesky post

Preview

Copy link

vercel bot commented Dec 17, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
19-react-dev ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 23, 2024 8:22pm
react-dev ✅ Ready (Inspect) Visit Preview Dec 23, 2024 8:22pm

Copy link

github-actions bot commented Dec 17, 2024

Size changes

📦 Next.js Bundle Analysis for react-dev

This analysis was generated by the Next.js Bundle Analysis action. 🤖

This PR introduced no changes to the JavaScript bundle! 🙌

@mattcarrollcode mattcarrollcode changed the title Add hanging ref troubleshooting section Add troubleshooting section for hanging refs when using ref callback cleanup functions Dec 17, 2024
@mattcarrollcode mattcarrollcode marked this pull request as ready for review December 17, 2024 10:23
Copy link
Member

@gaearon gaearon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see the second part of #7372 (comment). don't add troubleshooting just yet, instead add a recipe to useRef usage docs

@rickhanlonii
Copy link
Member

+1 to @gaearon's feedback, just to add some more context: we have a usage example in the Learn docs and we also explain how Strict Mode helps catch this bug, but having it on this page too would be useful.

In that thread I was actually saying we should document the behavior where callback refs unmount and remount when the callback function reference changes (so you should wrap it in useCallback) which isn't mentioned anywhere in the docs. Here's a sandbox showing the difference with/without useCallback.

@mattcarrollcode
Copy link
Contributor Author

In that thread I was actually saying we should document the behavior where callback refs unmount and remount when the callback function reference changes (so you should wrap it in useCallback) which isn't mentioned anywhere in the docs. Here's a sandbox showing the difference with/without useCallback.

Thanks for the CodeSandbox! I did see that and was planning on adding a mention of this as well but I wrote this content to better understand the problem space for myself. I'll add this in this PR or a followup.

@mattcarrollcode mattcarrollcode marked this pull request as draft December 21, 2024 08:16
@mattcarrollcode mattcarrollcode changed the title Add troubleshooting section for hanging refs when using ref callback cleanup functions Add useRef usage example for detecting DOM changes Dec 23, 2024
Copy link
Contributor Author

@mattcarrollcode mattcarrollcode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rewrote this PR to be a usage example in useRef for detecting DOM changes

src/content/reference/react-dom/components/common.md Outdated Show resolved Hide resolved
src/content/reference/react-dom/components/common.md Outdated Show resolved Hide resolved
src/content/reference/react-dom/components/common.md Outdated Show resolved Hide resolved
@mattcarrollcode mattcarrollcode marked this pull request as ready for review December 23, 2024 20:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants