blog/avoiding-use-effect-with-callback-refs #83
Replies: 24 comments 19 replies
-
Incredible explanation. I never thought of that before, thank you so much for sharing your knowledge! 🚀 |
Beta Was this translation helpful? Give feedback.
-
Wow thanks |
Beta Was this translation helpful? Give feedback.
-
Great writeup! This pattern looks really neat and super useful, thanks! const ref = React.useCallback((node) => {
node?.focus()
}, [])
return <input ref={ref} defaultValue="Hello world" /> |
Beta Was this translation helpful? Give feedback.
-
Thanks for this, I’ve always been scared of callbackRefs until today |
Beta Was this translation helpful? Give feedback.
-
Bravo! Great explanation on how to use this library in an efficient way. Sometimes I feel like we see one way of implementing a common problem and glob onto it. This provides a seemingly stable and safe alternative! Cheers🎉 |
Beta Was this translation helpful? Give feedback.
-
In my case I'd simply put the whole function in event listeners. |
Beta Was this translation helpful? Give feedback.
-
Thanks!! |
Beta Was this translation helpful? Give feedback.
-
Great one dodo 🤩, Thanks for writing |
Beta Was this translation helpful? Give feedback.
-
Really appreciate the tip! Good point that the ref may not be populated yet. I do have a concern about the solution though - I remember reading in React docs that referential stability in useCallback is not a guarantee; the memo hooks are performance optimizations, but they’re not designed to semantically represent the fact that they’d return the same reference each time. So instead of using memoization, React suggests to use refs! So what that means here is that you’d probably want a ref-ref instead of a callback-ref 😅 The plot thickens, as I’m betting the same principle applies regarding the behavior of the ref prop - if that’s purely a performance optimization and not a semantic guarantee, then you shouldn’t rely on that behavior being stable either. So if that’s true you’d need to check that manually. Annnddd at this point we might want a custom hook. React is fun sometimes… |
Beta Was this translation helpful? Give feedback.
-
I wish I know this already. This would have solved many of my annoying problems in the past. |
Beta Was this translation helpful? Give feedback.
-
nice one! your articles are valuable; very interesting approach :) |
Beta Was this translation helpful? Give feedback.
-
Many many thanks |
Beta Was this translation helpful? Give feedback.
-
As always, on point. |
Beta Was this translation helpful? Give feedback.
-
Super, very useful solution. Thank you |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot for this explanation! |
Beta Was this translation helpful? Give feedback.
-
Doesn't useLayoutEffect specifically for this use case? |
Beta Was this translation helpful? Give feedback.
-
Nice article. Learnt about callback refs and why it is better to use it over ref objects in some cases. Thank you for your time and effort for making this! |
Beta Was this translation helpful? Give feedback.
-
So using useLayoutEffect for focusing inputs is wrong? https://blog.octo.com/why-you-should-use-uselayouteffect-when-tinkering-with-focus/ |
Beta Was this translation helpful? Give feedback.
-
Any ideas how we can type a heading that need focus
I get 'Property 'focus' does not exist on type 'RefObject' or Property 'focus' does not exist on type 'RefObject' |
Beta Was this translation helpful? Give feedback.
-
Would you use a ref callback to update a css variable also? Or would you rather use useLayoutEffect/useEffect? const setContainerOffset = (container) => {
if (!container) return;
const { y } = container.getBoundingClientRect();
document.documentElement.style.setProperty("--offset", `${y}px`);
};
const MainPage = () => {
return <div ref={setContainerOffset} />
} |
Beta Was this translation helpful? Give feedback.
-
This works awesome on initial render. I also need to update the width on a resize as well. Ideas on how to do that inside the useEffect watching for the resize?
|
Beta Was this translation helpful? Give feedback.
-
I hope I've read this before |
Beta Was this translation helpful? Give feedback.
-
Great post. Thanks for explaining. |
Beta Was this translation helpful? Give feedback.
-
how could you give this a special ref type? like a type of HTMLDivElement for example? |
Beta Was this translation helpful? Give feedback.
-
blog/avoiding-use-effect-with-callback-refs
Interacting with DOM nodes doesn't necessarily need useEffect
https://tkdodo.eu/blog/avoiding-use-effect-with-callback-refs
Beta Was this translation helpful? Give feedback.
All reactions