Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Living wiki of UI design patterns and best practices built with Fumadocs, Next.js, and Base UI components.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
rules/container-callback-ref.md
1---2title: Use Callback Ref for Measurement3impact: MEDIUM4tags: container, ref, callback5---67## Use Callback Ref for Measurement89Use a callback ref (not useRef) for measurement hooks so the observer attaches when the DOM node is ready.1011**Incorrect (useRef may be null on first effect):**1213```tsx14const ref = useRef(null);15useEffect(() => {16if (!ref.current) return;17observer.observe(ref.current);18}, []);19```2021**Correct (callback ref guarantees node):**2223```tsx24const [element, setElement] = useState(null);25const ref = useCallback((node) => setElement(node), []);26useEffect(() => {27if (!element) return;28observer.observe(element);29return () => observer.disconnect();30}, [element]);31```32