<?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 Amrutesh Arun on Medium]]></title>
        <description><![CDATA[Stories by Amrutesh Arun on Medium]]></description>
        <link>https://medium.com/@amaru333?source=rss-475c29ade776------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*q2DWS7PHrbvVbMVosvFa1A.jpeg</url>
            <title>Stories by Amrutesh Arun on Medium</title>
            <link>https://medium.com/@amaru333?source=rss-475c29ade776------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 22:35:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@amaru333/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[How to Create Image Previews in a React Application Using a Custom Hook]]></title>
            <link>https://amaru333.medium.com/how-to-create-image-previews-in-a-react-application-using-a-custom-hook-94a77f693fa7?source=rss-475c29ade776------2</link>
            <guid isPermaLink="false">https://medium.com/p/94a77f693fa7</guid>
            <category><![CDATA[image]]></category>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[preview]]></category>
            <dc:creator><![CDATA[Amrutesh Arun]]></dc:creator>
            <pubDate>Thu, 13 Mar 2025 00:13:32 GMT</pubDate>
            <atom:updated>2025-03-13T00:13:32.099Z</atom:updated>
            <content:encoded><![CDATA[<p>When working with file uploads in a React application, it’s often useful to display a <strong>preview of the selected image</strong> before uploading it. This provides users with immediate feedback, ensuring they have selected the correct file.</p><p>Instead of manually handling image previews with FileReader or URL.createObjectURL, we can simplify the process using a <strong>custom React Hook</strong>. In this article, we’ll explore how to use <strong>@hookies/preview</strong>, a lightweight library that generates image previews effortlessly.</p><h3>Why Use a Custom Hook for Image Previews?</h3><p>Handling image previews in React often requires:</p><ul><li>Managing <strong>temporary object URLs</strong> for images</li><li>Cleaning up memory when a file changes</li><li>Keeping the preview <strong>reactive</strong> and updating automatically</li></ul><p>By using a <strong>custom hook</strong>, we can encapsulate this logic into a reusable function that can be used across multiple components.</p><h3>Step 1: Install the @hookies/preview Library</h3><p>To get started, install the package:</p><pre>npm install @hookies/preview</pre><p>or using yarn:</p><pre>yarn add @hookies/preview</pre><p>Once installed, you can start using the usePreview hook in your components.</p><h3>Step 2: Implementing Image Previews with usePreview</h3><p>The usePreview hook allows you to generate a <strong>preview URL</strong> for an uploaded file.</p><h3>Example: Displaying an Image Preview</h3><pre>import React, { useState } from &quot;react&quot;;<br>import { usePreview } from &quot;@hookies/preview&quot;;<br>const ImageUploader = () =&gt; {<br>  const [data, setData] = useState&lt;{ image?: File }&gt;({});<br>  const previewImageUrl = usePreview(data.image);<br>  const handleFileChange = (event: React.ChangeEvent&lt;HTMLInputElement&gt;) =&gt; {<br>    if (event.target.files &amp;&amp; event.target.files.length &gt; 0) {<br>      setData({ image: event.target.files[0] });<br>    }<br>  };<br>  return (<br>    &lt;div&gt;<br>      &lt;input type=&quot;file&quot; accept=&quot;image/*&quot; onChange={handleFileChange} /&gt;<br>      {previewImageUrl &amp;&amp; &lt;img src={previewImageUrl} alt=&quot;Preview&quot; width=&quot;200&quot; /&gt;}<br>    &lt;/div&gt;<br>  );<br>};<br>export default ImageUploader;</pre><h3>How It Works</h3><ol><li>The user <strong>selects an image file</strong> from the input field.</li><li>The usePreview hook generates a <strong>temporary preview URL</strong> using URL.createObjectURL.</li><li>The &lt;img&gt; tag updates dynamically to <strong>display the preview</strong>.</li><li>When the file changes, the <strong>previous preview URL is revoked</strong> to free up memory.</li></ol><h3>Conclusion</h3><p>Using a custom React Hook like usePreview simplifies image previews in your application. Instead of manually handling file previews and memory cleanup, you can use this <strong>reusable and optimized</strong> solution.</p><p>For updates and contributions, check out <a href="https://github.com/Amaru333/-hookies-preview">GitHub</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=94a77f693fa7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Add Keyboard Shortcuts in a React Application]]></title>
            <link>https://amaru333.medium.com/how-to-add-keyboard-shortcuts-in-a-react-application-e10826052f8f?source=rss-475c29ade776------2</link>
            <guid isPermaLink="false">https://medium.com/p/e10826052f8f</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[shortcuts]]></category>
            <category><![CDATA[keyboard-shortcuts]]></category>
            <category><![CDATA[hooks-in-react]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Amrutesh Arun]]></dc:creator>
            <pubDate>Wed, 12 Mar 2025 14:09:54 GMT</pubDate>
            <atom:updated>2025-03-12T14:09:54.984Z</atom:updated>
            <content:encoded><![CDATA[<p>Keyboard shortcuts can significantly improve the user experience by allowing users to perform actions quickly without navigating through menus or buttons. Implementing keyboard shortcuts in a React application can be done efficiently using <a href="https://www.npmjs.com/package/@hookies/key-bindings"><strong>@hookies/key-bindings</strong></a>, a lightweight library designed to handle both modifier-based and custom key combinations.</p><p>In this guide, we will explore how to integrate keyboard shortcuts into a React application using this library.</p><h3>Why Add Keyboard Shortcuts?</h3><p>Keyboard shortcuts provide several benefits:</p><ul><li><strong>Improved Accessibility:</strong> Users who rely on keyboard navigation can perform actions more efficiently.</li><li><strong>Increased Productivity:</strong> Common actions such as saving, copying, or toggling views can be done quickly.</li><li><strong>Enhanced User Experience:</strong> Power users appreciate the ability to interact with an application without excessive clicks.</li></ul><h3>Setting Up the Library</h3><p>To get started, install <a href="https://www.npmjs.com/package/@hookies/key-bindings">@hookies/key-bindings</a> in your project:</p><pre>npm install @hookies/key-bindings</pre><p>or using yarn:</p><pre>yarn add @hookies/key-bindings</pre><p>Once installed, you can import the required hooks to start adding keyboard shortcuts.</p><h3>Adding Modifier-Based Shortcuts</h3><p>For keyboard shortcuts involving <strong>Ctrl, Shift, Alt, or Meta (Command on Mac)</strong>, you can use the useShortcut hook.</p><h3>Example: Implementing a Save Shortcut</h3><p>The following example demonstrates how to trigger a function when a user presses <strong>Ctrl + S</strong>.</p><pre>import React, { useState } from &quot;react&quot;;<br>import { useShortcut } from &quot;@hookies/key-bindings&quot;;<br><br>const App = () =&gt; {<br>  const [message, setMessage] = useState(&quot;Press Ctrl + S&quot;);<br><br>  useShortcut([&quot;Ctrl&quot;, &quot;S&quot;], () =&gt; setMessage(&quot;Save triggered&quot;));<br><br>  return &lt;h1&gt;{message}&lt;/h1&gt;;<br>};<br><br>export default App;</pre><p>In this example:</p><ul><li>The useShortcut hook listens for <strong>Ctrl + S</strong>.</li><li>When the keys are pressed, the function updates the state with a new message.</li></ul><p>If you want to prevent the default browser behavior (e.g., <strong>Ctrl + S</strong> from opening the save dialog), you can pass an additional option:</p><pre>useShortcut([&quot;Ctrl&quot;, &quot;S&quot;], () =&gt; console.log(&quot;Save triggered&quot;), { preventDefault: true });</pre><h3>Adding Custom Key Combinations</h3><p>Some applications require detecting <strong>non-modifier key combinations</strong>, such as <strong>A + S</strong>, <strong>X + Y + Z</strong>, or <strong>1 + 2 + 3</strong>. This can be achieved using the useShortcutExtended hook.</p><h3>Example: Detecting Any Key Combination</h3><pre>import { useState } from &quot;react&quot;;<br>import { useShortcutExtended } from &quot;@hookies/key-bindings&quot;;<br><br>const App = () =&gt; {<br>  const [message, setMessage] = useState(&quot;Press A + S&quot;);<br><br>  useShortcutExtended([&quot;A&quot;, &quot;S&quot;], () =&gt; setMessage(&quot;Shortcut Triggered!&quot;));<br><br>  return &lt;h1&gt;{message}&lt;/h1&gt;;<br>};<br><br>export default App;</pre><p>Here:</p><ul><li>The useShortcutExtended hook listens for <strong>A + S</strong>.</li><li>When the combination is pressed, the function updates the message.</li></ul><p>This is particularly useful for applications that involve <strong>custom shortcuts</strong> beyond standard system-defined commands.</p><h3>Handling OS-Specific Shortcuts</h3><p>Keyboard shortcuts may vary based on the user’s operating system. For instance:</p><ul><li>Windows/Linux users often use <strong>Ctrl</strong> for shortcuts.</li><li>Mac users use <strong>Command (Meta)</strong> instead of <strong>Ctrl</strong>.</li></ul><p>To adjust shortcuts dynamically, you can use the getOS function:</p><pre>import { useShortcut, getOS } from &quot;@hookies/key-bindings&quot;;<br><br>const os = getOS();<br>const shortcutKeys = os === &quot;MacOS&quot; ? [&quot;Meta&quot;, &quot;C&quot;] : [&quot;Ctrl&quot;, &quot;C&quot;];<br><br>useShortcut(shortcutKeys, () =&gt; console.log(&quot;Copy triggered&quot;));</pre><p>This ensures:</p><ul><li><strong>Mac users</strong> can press <strong>Command + C</strong>.</li><li><strong>Windows/Linux users</strong> can press <strong>Ctrl + C</strong>.</li></ul><h3>Testing Shortcuts with the Playground</h3><p>If you want to test different key combinations before implementing them, visit the <strong>@hookies/key-bindings Playground</strong>:<br><a href="https://amaru333.github.io/hookies/components/key-bindings#playground"><strong>Test Shortcuts Online</strong></a></p><ul><li>Press any key combination (e.g., <strong>Meta + X</strong>, <strong>Ctrl + Shift + A</strong>).</li><li>The playground will generate the corresponding function:</li></ul><pre>useShortcut([&quot;meta&quot;, &quot;x&quot;], func);</pre><p>Copy and use it directly in your project.<br>This makes it easier to experiment with different keyboard shortcuts before adding them to your codebase.</p><h3>Use Cases for Keyboard Shortcuts</h3><p>Keyboard shortcuts are useful in various scenarios:</p><ul><li><strong>Text Editors:</strong> Implement <strong>Ctrl + S</strong> for saving, <strong>Ctrl + Z</strong> for undo, and <strong>Ctrl + Y</strong> for redo.</li><li><strong>Dashboards &amp; Admin Panels:</strong> Allow users to <strong>toggle menus, switch between views, or trigger actions</strong> quickly.</li><li><strong>Gaming Interfaces:</strong> Assign <strong>custom key bindings</strong> for player actions.</li><li><strong>Music &amp; Video Apps:</strong> Implement shortcuts for <strong>play, pause, volume controls</strong>.</li><li><strong>E-Commerce Sites:</strong> Enable shortcuts for <strong>search, cart, checkout actions</strong>.</li></ul><h3>Conclusion</h3><p>Keyboard shortcuts are an essential feature for enhancing user experience and productivity. With <strong>@hookies/key-bindings</strong>, adding keyboard shortcuts to your React application becomes effortless. Whether you need simple modifier-based shortcuts or complex key combinations, this library provides a flexible and lightweight solution.</p><p>To get started, install the package and explore the <a href="https://amaru333.github.io/hookies/components/key-bindings#playground">interactive playground</a> to generate shortcut functions dynamically.</p><p>If you find this useful, consider starring the repository on <a href="https://github.com/Amaru333/-hookies-key-bindings">GitHub</a> and contributing to its development.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e10826052f8f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Difference between visibility: hidden and display: none]]></title>
            <link>https://amaru333.medium.com/difference-between-visibility-hidden-and-display-none-e49fef8c33a7?source=rss-475c29ade776------2</link>
            <guid isPermaLink="false">https://medium.com/p/e49fef8c33a7</guid>
            <category><![CDATA[html]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Amrutesh Arun]]></dc:creator>
            <pubDate>Mon, 29 Aug 2022 14:27:42 GMT</pubDate>
            <atom:updated>2022-08-29T14:27:42.909Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Visibility hidden vs display none" src="https://cdn-images-1.medium.com/max/491/1*qrBgehLPc65pyb_x8xaAuw.png" /></figure><p>Though these two might sound the same, <strong>visibility: hidden</strong> and <strong>display: none</strong> works differently and can be used for several different cases. Let’s see the difference between them and their use cases here.</p><h3>Visibility: hidden</h3><p>When you use this CSS property on an element, it will be <strong>hidden from the screen but takes up the screen’s space</strong>. So the contents present before and after the element containing this property will be rendered, but it will leave the exact amount of space for the hidden element to occupy. The <strong>HTML element is still present in the DOM</strong> but the <strong>element won’t show up on the screen and takes up space</strong>. As per <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/visibility#syntax">MDN Documentation</a>:</p><blockquote>The element box is invisible (not drawn), but still affects layout as normal. Descendants of the element will be visible if they have visibility set to visible. The element cannot receive focus (such as when navigating through tab indexes).</blockquote><h3>Display: none</h3><p>On the other hand, display: none does a similar job, by<strong> hiding the element from the screen</strong>. But in this case, <strong>it won’t leave any space</strong> and the behavior feels like the element having this property doesn’t exist. Note that <strong>this property has no effect on the DOM and the element is still present in the DOM</strong>. It’s just that the element is hidden inside the DOM. You can’t remove an element from DOM using this CSS property.</p><h3>Use cases</h3><h4>Visibility: hidden</h4><p>Visibility: hidden can be used in situations where you want to hide an element but show it after performing an action, but keep enough space for it so that you don’t shift your whole layout once the action is performed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*7yjKYi2GajCrJ3P0Xi-88w.gif" /></figure><h4>Display: none</h4><p>Display: none can be used in the cases where you’ll have to display certain elements and hide the others based on what the user wants to see. One such example is Tab contents where the user selects a particular tab, and the contents under it are rendered based on the currently selected tab. The selected tab’s content is set to display: block or whatever the use case is, while the other contents are set to display: none. The property gets toggled every time the particular tab is changed.</p><figure><img alt="Display none" src="https://cdn-images-1.medium.com/max/488/1*YPwxb-a87opTNYgfGMnj_w.gif" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e49fef8c33a7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Infinite scrolling in React Native from API data in Redux without libraries]]></title>
            <link>https://amaru333.medium.com/pagination-infinite-loading-in-react-native-with-redux-and-api-6f6253b55927?source=rss-475c29ade776------2</link>
            <guid isPermaLink="false">https://medium.com/p/6f6253b55927</guid>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[infinite-scroll]]></category>
            <category><![CDATA[pagination]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <dc:creator><![CDATA[Amrutesh Arun]]></dc:creator>
            <pubDate>Sat, 20 Aug 2022 11:26:46 GMT</pubDate>
            <atom:updated>2022-08-21T14:57:42.416Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XUxSC_VCwWcwXHEV" /><figcaption>Photo by <a href="https://unsplash.com/@casparrubin?utm_source=medium&amp;utm_medium=referral">Caspar Camille Rubin</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In this article, I’ll explain how to create infinite scrolling in your React Native Application by making API calls through Redux and storing/retrieving data from Redux, without the necessity of any external libraries.</p><p>For this, we’ll be using the FlatList component to render the data and onEndReached prop, which triggers the function passed into it as soon as the rendered list reaches a scroll threshold.</p><p>Here’s a working example of what the final product would look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/388/1*NEXE4CrVKoNrFDp7cNA1bQ.gif" /><figcaption>Infinite scrolling with the help of FlatList</figcaption></figure><p>Firstly, your API needs to support pagination. I’ve coded my API in such a way that I’ll have to send a limit and skip queries to get the right amount of data in response.</p><p>As we’re using Redux Toolkit, let’s first make a service file. In my case, I’ll name it productService.js</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/26dfa9ba434b85cf5666b4042064c3f1/href">https://medium.com/media/26dfa9ba434b85cf5666b4042064c3f1/href</a></iframe><p>The function getProducts will return a response containing:</p><ol><li>The maxCount value which represents the total number of documents present in the collection, and is also based on any external queries provided.</li><li>An array of objects which contains the data. The length of the array depends on what limit has been passed in the query</li></ol><p>Now let’s create a productSlice, to store all our data. At first, we need to create an initial state:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe1aa06987407a093fc4c322c23c3bb4/href">https://medium.com/media/fe1aa06987407a093fc4c322c23c3bb4/href</a></iframe><p>Then, we need to create a function using createAsyncThunk method, where we’ll call our getProducts() function which we had defined in our productService.js file. This will return an object containing a field called “response_data” which has the response from the API call, as well as we’ll be storing our query data which we’re gonna call from the main component later.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e079e56701e73dcc1b6768a4eecc359e/href">https://medium.com/media/e079e56701e73dcc1b6768a4eecc359e/href</a></iframe><p>And finally, we’ll have to create a slice for our products redux store. Here we’ll write all our logics inside extraReducers, as we’ll be using createAsyncThunk to get our data. Once the request is fulfilled, we’re gonna push the array of response objects into our initialState’s data array.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c6fd321a5bf514461a9a5c5e0b20a266/href">https://medium.com/media/c6fd321a5bf514461a9a5c5e0b20a266/href</a></iframe><p>In the end, your slice would look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5efaf48481675ae820d8de15f12d5737/href">https://medium.com/media/5efaf48481675ae820d8de15f12d5737/href</a></iframe><p>I’ve used the reselect library to create selectors so that I can use only certain data required in my component. This isn’t required.</p><p>Now we need to configure to add up all the redux stores which we’ve created, in this case, it’s just productSlice.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bb0bdd6049197d376ee0112d9712b62b/href">https://medium.com/media/bb0bdd6049197d376ee0112d9712b62b/href</a></iframe><p>Now finally we’re at the part where we implement the logics in our component.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/574109aec4b01c6a60b8a13c792dcf91/href">https://medium.com/media/574109aec4b01c6a60b8a13c792dcf91/href</a></iframe><p>I’ve hardcoded the Page Limit to be set as 10. Initially, the useEffect calls the “getProducts” function with page=0, limit=10 and skip=0. This will get the first 10 items from the collection and store them inside redux. After this, whenever the user scrolls down and reaches to the end of the list, the “getProducts” function gets called, but this time, the page and skip parameters value changes. It changes dynamically based on it’s updated value in the redux store. Every time this function gets called, if the API call is successfully performed, the page number gets updated in redux. The function gets called only if the total count is greater than (current page number * limit).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6f6253b55927" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>