react-hotkeys-hook icon indicating copy to clipboard operation
react-hotkeys-hook copied to clipboard

React to ref changes

Open zeorin opened this issue 1 year ago • 3 comments

  • 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.

zeorin avatar Feb 08 '24 14:02 zeorin

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

vercel[bot] avatar Feb 08 '24 14:02 vercel[bot]

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

vilemj-Viclick avatar Feb 15 '24 09:02 vilemj-Viclick

@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.

zeorin avatar Apr 18 '24 08:04 zeorin

Sorry that it took so long, thank you for your PR!

JohannesKlauss avatar Aug 19 '24 15:08 JohannesKlauss