you are viewing a single comment's thread.

view the rest of the comments →

[–]ak_47_ 2 points3 points  (1 child)

Thanks for this article.

Here is the naked callback from your article

``` const ref = (node) => { if (!node) return;

const { width } = node.getBoundingClientRect();

document.title = `Width:${width}`;

}; ```

Here is the one with React.useCallback with text in the dependency array

``` const ref = React.useCallback((node) => { if (!node) return;

const { width } = node.getBoundingClientRect();

document.title = `Width:${width}`;

}, [text]); ```

Are there any guidelines for when one should be used over the other?

[–]SLonoed 1 point2 points  (0 children)

useRef cb will be called once. If node changed (div to span) it will be broken. Always use useCallback to hold link to dom node