react-hotkeys-hook
react-hotkeys-hook copied to clipboard
React to ref changes
- 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
setStatefunction directly as a callback ref function.
I reused the test from #1117.
Fixes #1116.
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 |
Nice! I'm glad you found the test useful. I'll see to it that my Pull Request #1117 is abandoned.
@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.
Sorry that it took so long, thank you for your PR!