<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Samith Dilhara</title>
    <description>The latest articles on DEV Community by Samith Dilhara (@samithf).</description>
    <link>https://dev.to/samithf</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F477927%2Ff5dda007-f0af-4012-9391-6964fe3078f0.jpg</url>
      <title>DEV Community: Samith Dilhara</title>
      <link>https://dev.to/samithf</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/samithf"/>
    <language>en</language>
    <item>
      <title>usage of useImperativeHandle hook</title>
      <dc:creator>Samith Dilhara</dc:creator>
      <pubDate>Tue, 19 Jan 2021 17:34:41 +0000</pubDate>
      <link>https://dev.to/samithf/usage-of-useimperativehandle-hook-4eh0</link>
      <guid>https://dev.to/samithf/usage-of-useimperativehandle-hook-4eh0</guid>
      <description>&lt;p&gt;Before we move into the usage of &lt;code&gt;useImperativeHandle&lt;/code&gt;, let’s discuss the problem this hook is trying to fix.&lt;/p&gt;

&lt;p&gt;Here we have defined the App component and Input component, The goal of the application is to focus the input when the button element is clicked.&lt;/p&gt;

&lt;p&gt;with a &lt;code&gt;forwardRef&lt;/code&gt; this is pretty simple right?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const App = () =&amp;gt; {
      const inputRef = useRef(null);

      const handleClick = () =&amp;gt; {
        inputRef.current.focus();
      };

      return (
        &amp;lt;&amp;gt;
          &amp;lt;Input ref={inputRef} /&amp;gt;
          &amp;lt;button onClick={handleClick}&amp;gt;click to focus child input&amp;lt;/button&amp;gt;
        &amp;lt;/&amp;gt;
      );
    }

    const Input = forwardRef((props, ref) =&amp;gt; {
      return &amp;lt;input ref={ref} {...props} /&amp;gt;;
    });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, in this solution, the parent component App has full access to the input element in Input component, the &lt;code&gt;inputRef&lt;/code&gt; declared in App holds the full DOM node for the child input element.&lt;/p&gt;

&lt;p&gt;but if you don't want to give full DOM node access to the parent, you can achieve it using &lt;code&gt;useImperativeHandle&lt;/code&gt; hook. you can just expose only the focus function for the parent&lt;/p&gt;

&lt;p&gt;Updated Input Component&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Input = forwardRef((props, ref) =&amp;gt; {
  // create internal ref object to hold actual input DOM node 
  const inputRef = useRef();

  // pass ref from parent to useImperativeHandle and replace its value with the createHandle function
  useImperativeHandle(ref, () =&amp;gt; ({
    focus: () =&amp;gt; {
      inputRef.current.focus();
    }
  }));

  // pass internal ref to input to hold DOM node
  return &amp;lt;input ref={inputRef} {...props} /&amp;gt;;
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;code&gt;useImperativeHandle&lt;/code&gt; accepts the parent ref and a callback function as arguments, the function should return an object to replace the &lt;code&gt;current&lt;/code&gt; property in parent ref, so &lt;code&gt;focus&lt;/code&gt; is the only function which App can access now.&lt;/p&gt;

&lt;p&gt;In this way, we can limit/expose functions/properties to parent components using ref  &lt;/p&gt;

</description>
      <category>react</category>
    </item>
  </channel>
</rss>
