<?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: Rakhi</title>
    <description>The latest articles on DEV Community by Rakhi (@atbrakhi).</description>
    <link>https://dev.to/atbrakhi</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%2F55442%2Ffd5860a5-0857-4dcb-a80c-6dfa98eda091.jpg</url>
      <title>DEV Community: Rakhi</title>
      <link>https://dev.to/atbrakhi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/atbrakhi"/>
    <language>en</language>
    <item>
      <title>I wrote an online iframe editor in React</title>
      <dc:creator>Rakhi</dc:creator>
      <pubDate>Sun, 25 Apr 2021 20:12:41 +0000</pubDate>
      <link>https://dev.to/atbrakhi/i-wrote-an-online-iframe-editor-in-react-1mcg</link>
      <guid>https://dev.to/atbrakhi/i-wrote-an-online-iframe-editor-in-react-1mcg</guid>
      <description>&lt;p&gt;Earlier this month, when I was writing my first blog post on this platform, I wanted to edit one of the iframes I was trying to embed in my blog. I was very surprised to see I couldn't find any dedicated online tool to test or edit the iframe. I ended up being annoyed by this idea.&lt;/p&gt;

&lt;p&gt;So, I wrote an online iframe editor. Check it out &lt;a href="https://www.iframeeditor.com/"&gt;iframe-editor&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check it out on github &lt;a href="https://github.com/Rakhisharma/online-iframe-editor"&gt;online-iframe-editor&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It provides the following functionalities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Test your iframe&lt;/li&gt;
&lt;li&gt;Edit your iframe&lt;/li&gt;
&lt;li&gt;Build your own iframe&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IVCKqRNw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6f725br59i16nkbuu8sk.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IVCKqRNw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6f725br59i16nkbuu8sk.gif" alt="online-iframe-editor-gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you would like to contribute or have any suggestion checkout the guidelines &lt;a href="https://github.com/Rakhisharma/online-iframe-editor"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Implement your own custom hook in React with typescript</title>
      <dc:creator>Rakhi</dc:creator>
      <pubDate>Sun, 25 Apr 2021 12:15:50 +0000</pubDate>
      <link>https://dev.to/atbrakhi/implement-your-own-custom-hook-in-react-with-typescript-1f1l</link>
      <guid>https://dev.to/atbrakhi/implement-your-own-custom-hook-in-react-with-typescript-1f1l</guid>
      <description>&lt;p&gt;As a beginner, thinking about creating a custom hook sounded very complex to me. As I spent more time working with them, I realized it isn't that complicated after all.&lt;/p&gt;

&lt;p&gt;In this blog post I am going to take a very simple example and  create my own custom hook out of it.&lt;/p&gt;

&lt;p&gt;I wrote a blog post recently about creating a toggle button in React &lt;a href="https://dev.to/atbrakhi/creating-a-toggle-button-in-react-i8o"&gt;here&lt;/a&gt;. In this blog post I am going to convert my toggle function into a react hook.&lt;/p&gt;

&lt;p&gt;Why am I writing this hook for this small function, is it even needed?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I want to show you how to create your own custom hook by giving you a simple example.&lt;/li&gt;
&lt;li&gt;Having a custom hook is useful as it hides information, which means you are utilizing encapsulation.&lt;/li&gt;
&lt;li&gt;It separates logic from the component. Your components will be super clean that way.&lt;/li&gt;
&lt;li&gt;Writing hooks means you are testing more and your code is more flexible. You can extend functionality without changing any component, in case the requirement changes as it always happens!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's go!&lt;/p&gt;

&lt;p&gt;If you read my blog &lt;a href="https://dev.to/atbrakhi/creating-a-toggle-button-in-react-i8o"&gt;Creating a Toggle button in React&lt;/a&gt;, you are good to continue reading. If not, I would highly recommend reading it, it will take &amp;lt;2 mins. I promise this blog post will look easier afterwards.&lt;/p&gt;

&lt;p&gt;Now, that you have read my previous blog post, you might have noticed my code for creating a toggle button looks like this:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/u74p9?view=editor"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;In order to create a custom hook out of it, we need to follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a folder name &lt;code&gt;Hooks&lt;/code&gt; and inside it create a file called &lt;code&gt;useToggle.ts&lt;/code&gt; (remember all hook names start with &lt;code&gt;use&lt;/code&gt;, let's keep the consistency)&lt;/li&gt;
&lt;li&gt;Implement the handler method in &lt;code&gt;useToggle&lt;/code&gt; file.&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;useToggle&lt;/code&gt; hook in your component.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let get started then!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;.  Create &lt;code&gt;Hooks&lt;/code&gt; folder and a file inside it, name it as &lt;code&gt;useToggle.ts&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;. Implement toggle handler function in the &lt;code&gt;useToggle.ts&lt;/code&gt; file:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add a state:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const [state, setState] = useState("off");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Write the handler function:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const handlers = () =&amp;gt; ({
    toggle: () =&amp;gt; {
      setState((res) =&amp;gt; (res === "on" ? "off" : "on"));
    }
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Memoize the handler function using &lt;code&gt;useMemo&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const handlers = useMemo(
    () =&amp;gt; ({
      toggle: () =&amp;gt; {
        setState((res) =&amp;gt; (res === "on" ? "off" : "on"));
      }
    }),
    []
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you must be wondering why we needed to memoize the function here?  By using &lt;code&gt;useMemo&lt;/code&gt; we are making sure our function remembers the result of the last time it was called. It also comes in very handy in performance optimizations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;. Use the hook &lt;code&gt;useToggle&lt;/code&gt; in the component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [toggleState, { toggle }] = useToggle();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all.&lt;/p&gt;

&lt;p&gt;Here is how our &lt;code&gt;useToggle&lt;/code&gt; hook looks like at the end.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/3ev58?module=/Hooks/useToggle.ts&amp;amp;view=editor"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Here is how our component looks at the end:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/3ev58?view=split"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Bookmark it in case you need it later or feel free to reach out &lt;a href="https://twitter.com/atbrakhi"&gt;atbrakhi&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>typescript</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Creating a Toggle button in React</title>
      <dc:creator>Rakhi</dc:creator>
      <pubDate>Fri, 02 Apr 2021 13:09:22 +0000</pubDate>
      <link>https://dev.to/atbrakhi/creating-a-toggle-button-in-react-i8o</link>
      <guid>https://dev.to/atbrakhi/creating-a-toggle-button-in-react-i8o</guid>
      <description>&lt;p&gt;Lately I was working on a problem where I had to toggle something in React JS and I couldn't find a simple 1-2 liner solution for that, like we have in Jquery. If you have ever used it, you probably know what I am talking about.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: I am not trying to compare &lt;code&gt;Jquery&lt;/code&gt; and &lt;code&gt;React&lt;/code&gt; here. They both serve their purpose very well.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's take an example. We will create a button that changes the text from &lt;code&gt;Off&lt;/code&gt; to &lt;code&gt;On&lt;/code&gt; and vice-versa with one click of a button.&lt;/p&gt;

&lt;p&gt;Let's talk through this example step by step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a button - we will use material-UI for that.&lt;/li&gt;
&lt;li&gt;Create a state that will hold the &lt;code&gt;On/Off&lt;/code&gt; value.&lt;/li&gt;
&lt;li&gt;Show &lt;code&gt;On/Off&lt;/code&gt; text once user clicks the button. &lt;/li&gt;
&lt;li&gt;If the text says &lt;code&gt;On&lt;/code&gt; and user clicks the button, it should   change the text to &lt;code&gt;Off&lt;/code&gt; and vice-versa.&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt; &lt;strong&gt;Step 1&lt;/strong&gt;: Create a button
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Button variant="contained" color="primary"&amp;gt;
    Toggle
&amp;lt;/Button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step 2&lt;/strong&gt;: Create a state that will hold value &lt;code&gt;On/Off&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [textState, setTextState] = useState("off");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are taking &lt;code&gt;Off&lt;/code&gt; as initial state to avoid any kind of code smell.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step 3&lt;/strong&gt;: Show &lt;code&gt;On/Off&lt;/code&gt; text on click of the button.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This part involves two small but important steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A text that changes with the click of the button&lt;/li&gt;
&lt;li&gt;A function to handle the toggle when user clicks on the button&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, let's pass the text first, &lt;code&gt;buttonState&lt;/code&gt; will give us &lt;code&gt;Off&lt;/code&gt; by default as it is our initial state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;h3&amp;gt;{textState}&amp;lt;/h3&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the Handler function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const toggleText = () =&amp;gt; {
    setTextState((state) =&amp;gt; (state === "On" ? "Off" : "On"));
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are updating the state  of the button here. If it is &lt;code&gt;On&lt;/code&gt;, change it to &lt;code&gt;Off&lt;/code&gt; and if it is &lt;code&gt;Off&lt;/code&gt;, change it to &lt;code&gt;On&lt;/code&gt;. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step 4&lt;/strong&gt;: Toggle, to achieve this we only need to pass our handler function to the &lt;code&gt;onClick&lt;/code&gt; of &lt;code&gt;&amp;lt;Button&amp;gt;&lt;/code&gt; from step 1. Like this:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Button variant="contained" color="primary" onClick={toggleText}&amp;gt;
    Toggle
&amp;lt;/Button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's all! Here is how it looks when we put all the above code together: &lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/u74p9?view=split"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>JavaScript Array.some() or Array.every()?</title>
      <dc:creator>Rakhi</dc:creator>
      <pubDate>Thu, 01 Apr 2021 18:58:09 +0000</pubDate>
      <link>https://dev.to/atbrakhi/javascript-array-some-or-array-every-4la1</link>
      <guid>https://dev.to/atbrakhi/javascript-array-some-or-array-every-4la1</guid>
      <description>&lt;p&gt;I recently started using &lt;code&gt;array.some()&lt;/code&gt; and at the same time also found &lt;code&gt;array.every()&lt;/code&gt; interesting. It saved time for me, might do the same to you!&lt;/p&gt;

&lt;p&gt;We will talk about both methods here.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Array.some()&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It will return &lt;code&gt;True&lt;/code&gt; if any element matches the required condition.&lt;/li&gt;
&lt;li&gt;You would want to use this method in case you want to check if at least one of the elements in the array matches the constraints.&lt;/li&gt;
&lt;li&gt;Array.some() will always return false if the array is empty. But then we ask why so? It is because array.some() returns true if at least one element of an array passes your condition. If the array is empty, none of the elements pass the condition and hence it returns false.&lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;
  
  
  Let's check out Array.some() examples:
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Check if there is a negative number in the array:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/zd03m?view=editor"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Check if value exists in the array:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/lxwxb?view=editor"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What if there is an empty array and you also need to use &lt;code&gt;array.some()&lt;/code&gt;? It is possible to do that but note it will always return &lt;code&gt;False&lt;/code&gt; as mentioned before:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/cvtvs?view=editor"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Array.every()&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It returns &lt;code&gt;True&lt;/code&gt; if all the elements in the array match your condition. &lt;/li&gt;
&lt;li&gt;You would want to use this method in case you want to check if all the elements in the array match your condition.&lt;/li&gt;
&lt;li&gt;It returns &lt;code&gt;True&lt;/code&gt; if the array is empty. Then we again ask, hey, why? It is because &lt;code&gt;array.every()&lt;/code&gt; returns true if every element of an array passes the condition. If there are no items in the array, every element in the array passes the condition, hence it returns true.&lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;
  
  
  Let's check out Array.every() examples:
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Check if every number in array is negative:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/8gq66?view=editor"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For empty array it will always return &lt;code&gt;True&lt;/code&gt;:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/qy2ef?view=editor"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

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