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

React to ref changes #1132

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

zeorin
Copy link

@zeorin zeorin commented Feb 8, 2024

  • Use a callback ref
  • Re-run the effect when the ref changes
  • Updated docs
    • Fix: TS doesn't actually infer the type of the ref automatically, this was already the case regardless of whether the ref is a ref object or a ref callback

When reacting to ref changes, it's easiest when:

  • the ref is a callback ref function, this way React will call your callback whenever the ref changes,
  • you store the instance React gives you in your callback in state. That way, when your callback is called, simply update the state, and anything that depends on it will just react as normal. Conveniently, one can just use the setState function directly as a callback ref function.

I reused the test from #1117.

Fixes #1116.

- Use a ref callback
- Re-run the effect when the ref changes
- Updated docs
  - Fix: TS doesn't actually infer the type of the ref
    automatically, this was already the case regardless of whether the
    ref is a ref object or a ref callback
Copy link

vercel bot commented Feb 8, 2024

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

Name Status Preview Comments Updated (UTC)
react-hotkeys-hook ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 8, 2024 2:37pm

@vilemj-Viclick
Copy link

Nice!
I'm glad you found the test useful. I'll see to it that my Pull Request #1117 is abandoned.

@zeorin
Copy link
Author

zeorin commented Apr 18, 2024

@JohannesKlauss I would be most grateful if you could review this PR. 🙏

In the meantime, I've worked around this by applying my changes to my project using yarn's package patching feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Attaching resulting ref to a different element stops the shortcuts/hotkeys from working
2 participants