<?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: Rutvik Umak</title>
    <description>The latest articles on DEV Community by Rutvik Umak (@rutvikumak).</description>
    <link>https://dev.to/rutvikumak</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%2F814897%2Fc4755fcb-c4f8-4b02-bdbf-72da3da3f0d7.jpeg</url>
      <title>DEV Community: Rutvik Umak</title>
      <link>https://dev.to/rutvikumak</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rutvikumak"/>
    <language>en</language>
    <item>
      <title>React useRef Hook</title>
      <dc:creator>Rutvik Umak</dc:creator>
      <pubDate>Wed, 11 May 2022 15:17:57 +0000</pubDate>
      <link>https://dev.to/rutvikumak/react-useref-hook-2hc4</link>
      <guid>https://dev.to/rutvikumak/react-useref-hook-2hc4</guid>
      <description>&lt;p&gt;&lt;strong&gt;Refs are probably the most misunderstood and misused part of React&lt;/strong&gt;. In this post I am going to cover everything you need to know about refs in order to help you never make those ref mistakes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So Let's get started ,&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkqu0e5xgelasn8qt3bf0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkqu0e5xgelasn8qt3bf0.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we already know that hooks in react is nothing but a function so , The useRef Hook is also a function which return an mutable object whose &lt;code&gt;.current&lt;/code&gt; property is initialized with passed argument (&lt;code&gt;initialValue&lt;/code&gt;).The returned object will persist for lifetime of the component.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;How to use in you react app ?&lt;/strong&gt;
&lt;/h2&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Step 1 :&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;import {useRef} from "react";&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2 :&lt;/strong&gt;&lt;br&gt;
In your functional component write&lt;br&gt;
&lt;code&gt;const myRef = useRef(0)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Internally the &lt;strong&gt;myRef&lt;/strong&gt; is now equal to an object that looks like &lt;code&gt;{current : 0}&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Let's learn better from example&lt;/strong&gt;
&lt;/h2&gt;
&lt;/blockquote&gt;

&lt;p&gt;We will take an example of an counter , and compare it using useState and useRef hooks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using useState&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://codesandbox.io/s/friendly-fast-0rwzhr?file=/src/App.js" rel="noopener noreferrer"&gt;Sandbox&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3eh5srwygmuns18ksgvf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3eh5srwygmuns18ksgvf.png" alt="useState Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using useRef&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://codesandbox.io/s/dazzling-spence-352sb6?file=/src/App.js" rel="noopener noreferrer"&gt;Sandbox&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyh8a8g2ez59ehqsl5zbu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyh8a8g2ez59ehqsl5zbu.png" alt="useRef Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Both of these components are doing the same work of incrementing the value of counter by 1, but in the state example the component will re-render itself since &lt;strong&gt;useState&lt;/strong&gt; causes the component to re-render. The ref example on the other hand will not update the value on the page because setting a ref wont cause &lt;strong&gt;re-render of component&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Now We have got a basic idea of how useRef basically works internally and lets now talk about when you would need to use a Ref.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54qyvxytvd6i4sriaczn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54qyvxytvd6i4sriaczn.png" alt="Focus Example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;u&gt;The most common use case for refs in React is to reference a DOM element. Because of how common this use case is every DOM element has a ref property you can use for setting a ref to that element.&lt;/u&gt; For example, if you wanted to focus an input element whenever a button was clicked you could use a ref to do that.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codesandbox.io/s/new-framework-zdxh1g?file=/src/App.js" rel="noopener noreferrer"&gt;Sandbox&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6bkc4tv5csoks6yt6tws.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6bkc4tv5csoks6yt6tws.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see in the code above we use the ref property on the input element to set the current value of inputRef to the input element. Now when we click the button it will call focusInput which uses the current value of the inputRef variable to set the focus on the input element.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Being able to access any DOM elements directly with ref is useful with setting focus and manage other attributes that you cannot directly achieve in react.&lt;/u&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;useRef won't re-render the component like useState do.&lt;/li&gt;
&lt;li&gt;If you want to do some operation that won't need re-rendering of component then you can use useRef hook.&lt;/li&gt;
&lt;li&gt;Refs are useful when getting user input, DOM element properties and storing constantly updating values.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks for reading !&lt;br&gt;
If you liked this blog, please share it with your friends !&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Resources&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://reactjs.org/docs/hooks-reference.html#useref" rel="noopener noreferrer"&gt;https://reactjs.org/docs/hooks-reference.html#useref&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://levelup.gitconnected.com/understanding-useref-513b1bbe00dc" rel="noopener noreferrer"&gt;https://levelup.gitconnected.com/understanding-useref-513b1bbe00dc&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/salehmubashar/useref-or-usestate-which-is-better-258j"&gt;https://dev.to/salehmubashar/useref-or-usestate-which-is-better-258j&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Connect with me&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://twitter.com/rutvikumak13" rel="noopener noreferrer"&gt;https://twitter.com/rutvikumak13&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/rutvikpumak" rel="noopener noreferrer"&gt;https://github.com/rutvikpumak&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>usere</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Why React is UI Library and not a Framework ?</title>
      <dc:creator>Rutvik Umak</dc:creator>
      <pubDate>Mon, 14 Feb 2022 04:12:12 +0000</pubDate>
      <link>https://dev.to/rutvikumak/why-react-is-ui-library-and-not-a-framework--4cal</link>
      <guid>https://dev.to/rutvikumak/why-react-is-ui-library-and-not-a-framework--4cal</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Understanding difference between Library and Framework&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;When we call something as a framework it has flexibility of doing multiple things at a time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Library is kind of focus on just one thing and get it done.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When we use framework , the framework is in charge of the flow.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When we use library , you are in charge of the library because you will be choosing when and where to call that library.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Now let's understand about React&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;React is User Interface Library because react was actually invented or designed coded to solve the problem of User Interface Development.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa03dcy2x2i1tgkkds1xq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa03dcy2x2i1tgkkds1xq.png" alt="Oustide function for React"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If react would have framework :&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;We would have routing inside built in , but we don't have.&lt;/li&gt;
&lt;li&gt;We would have testing framework built in , but we use something like jest or react test library.&lt;/li&gt;
&lt;li&gt;For data fetching we have to use axios or fetch api to achieve that.&lt;/li&gt;
&lt;li&gt;For Building , we have infrastructure by yourself or web pack.&lt;/li&gt;
&lt;li&gt;For state management , we have to redux or any other tool to do that.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Notification system or localization or anything , practically anything you want to do in react based application you have to bring it from outside.&lt;/p&gt;

&lt;p&gt;React is only meant for doing this , and that's great advantage because you have the liberty of what you want to use versus you don't want to use.&lt;/p&gt;

&lt;p&gt;According to your project need , you actually has the ability to choose that's why react is so powerful in terms of giving you the liberty, giving you the flexibility of doing things in you way.&lt;/p&gt;

&lt;p&gt;You must just focus on building things and bringing things whatever you need and yes that's why &lt;/p&gt;

&lt;h2&gt;
  
  
  "React is Library and not a Framework"
&lt;/h2&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
