<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Daniel Schmidt on Medium]]></title>
        <description><![CDATA[Stories by Daniel Schmidt on Medium]]></description>
        <link>https://medium.com/@teh_builder?source=rss-696970554ce8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*HS85sKdOEdCT4tsBIu2giw.jpeg</url>
            <title>Stories by Daniel Schmidt on Medium</title>
            <link>https://medium.com/@teh_builder?source=rss-696970554ce8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 00:23:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@teh_builder/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Ref objects inside useEffect Hooks]]></title>
            <link>https://medium.com/@teh_builder/ref-objects-inside-useeffect-hooks-eb7c15198780?source=rss-696970554ce8------2</link>
            <guid isPermaLink="false">https://medium.com/p/eb7c15198780</guid>
            <category><![CDATA[ref]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[ref-callback]]></category>
            <category><![CDATA[hooks]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Daniel Schmidt]]></dc:creator>
            <pubDate>Mon, 11 Feb 2019 08:50:02 GMT</pubDate>
            <atom:updated>2019-10-16T21:59:35.505Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ed1p7Cxo2Bj8WJmX6xnF8Q@2x.jpeg" /></figure><p>When making a custom hook that needs a reference to a DOM node, your first guess would be to use the new useRef hook. It was certainly mine.</p><p>So naturally I went with that for my first implementation of Hooks in <a href="https://github.com/thebuilder/react-intersection-observer">react-intersection-observer</a> — But then an interesting issue came up, that I hadn’t considered.</p><p><a href="https://github.com/thebuilder/react-intersection-observer/issues/162">[Question] How would you handle conditional rendering with hooks? · Issue #162 · thebuilder/react-intersection-observer</a></p><p>Essentially, I was assuming that a ref would always be defined, but —</p><ul><li>ref could be undefined to begin with, and then set later.</li><li>ref could change to a different element.</li><li>ref could change from an element to undefined.</li></ul><p>My hook wasn’t handling any of these cases.</p><h3>The problem with refs and useEffect</h3><p>The useRef hook can be a trap for your custom hook, if you combine it with a useEffect that skips rendering. Your first instinct will be to add ref.current to the second argument of useEffect, so it will update once the ref changes.</p><p>But the ref isn’t updated till after your component has rendered — meaning, any useEffect that skips rendering, won’t see any changes to the ref before the next render pass.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2F01m0ok09rv%3Fhidenavigation%3D1&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2F01m0ok09rv%3Fhidenavigation%3D1&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2F01m0ok09rv%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/95dd6fa29d6b9a7eeeb9322b512b015f/href">https://medium.com/media/95dd6fa29d6b9a7eeeb9322b512b015f/href</a></iframe><p>Looking at the community created hooks, this seems like a fairly widespread issue right now. Great Hook collections like <a href="https://usehooks.com/">usehooks.com</a> and <a href="https://github.com/palmerhq/the-platform">palmerhq/the-platform</a> falls into this trap — most of the time because they assume the ref won’t change, and it’s set during the first render.</p><p>I went ahead and asked Dan Abramov how to solve this issue:</p><h3>Dan Abramov on Twitter</h3><p>@teh_builder I think you want callback ref for that. You can&#39;t have component magically react to ref changes because ref can go deep down and have independent lifecycle of the owner component.</p><p>The React team <a href="https://reactjs.org/docs/hooks-effect.html#explanation-why-effects-run-on-each-update">did try to warn us</a> about errors if we skip effects.</p><h3>The solution</h3><p>Instead of useRef, you should create a <a href="https://reactjs.org/docs/refs-and-the-dom.html#callback-refs">ref callback</a> with useCallback.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b1b166d7f52caa31025d35d19021a618/href">https://medium.com/media/b1b166d7f52caa31025d35d19021a618/href</a></iframe><p>Once the callback is triggered with a node, you can consume it as needed.</p><blockquote>For a complete example, have a look at the <a href="https://github.com/thebuilder/react-intersection-observer/blob/master/src/useInView.tsx">useInView</a> source code for <a href="https://github.com/thebuilder/react-intersection-observer">react-intersection-observer</a>.</blockquote><h3>Official Solution</h3><p>The React FAQ has now been updated with an example of using a ref callback to measure the size of DOM elements. This is also a solution to this exact problem, so make sure to read that entry.</p><p><a href="https://reactjs.org/docs/hooks-faq.html#how-can-i-measure-a-dom-node">Hooks FAQ - React</a></p><p>You could argue that hooks can assume the ref is set when created, but I’ve already seen multiple cases of developers running into this issue, unsure of why their hook isn’t working. Seeing how simple the solution is, I think it’s important that all custom hooks start to implement it.</p><p>Keep in mind this is only a problem if your useEffect hook doesn’t trigger on all renders, so keep using useRef in those hooks.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eb7c15198780" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>