<?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 Narayan  Dubey on Medium]]></title>
        <description><![CDATA[Stories by Narayan  Dubey on Medium]]></description>
        <link>https://medium.com/@narayan.k.dubey?source=rss-eed3a16616cd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Narayan  Dubey on Medium</title>
            <link>https://medium.com/@narayan.k.dubey?source=rss-eed3a16616cd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 17:19:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@narayan.k.dubey/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[Building an OTP Screen in React]]></title>
            <link>https://medium.com/@narayan.k.dubey/building-an-otp-screen-in-react-55281936b93d?source=rss-eed3a16616cd------2</link>
            <guid isPermaLink="false">https://medium.com/p/55281936b93d</guid>
            <category><![CDATA[functional-component]]></category>
            <category><![CDATA[otp-verification]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Narayan  Dubey]]></dc:creator>
            <pubDate>Sun, 21 Jan 2024 07:46:27 GMT</pubDate>
            <atom:updated>2024-01-21T07:59:06.410Z</atom:updated>
            <content:encoded><![CDATA[<p>In this tutorial, we’ll walk through the process of creating a simple OTP (One-Time Password) screen in React. OTP screens are commonly used in authentication processes, and we’ll implement a basic version using React components.</p><h3>Prerequisites</h3><p>Make sure you have Node.js and npm installed on your machine. You can install them by following the instructions on the official <a href="https://nodejs.org/">Node.js website</a>.</p><h3>Getting Started</h3><p>Let’s start by setting up a new React application. Open your terminal and run the following commands:</p><pre>npx create-react-app react-otp-screen<br>cd react-otp-screen<br>npm start</pre><p>This will create a new React application and start the development server. Open your browser and navigate to http://localhost:3000 to see the default React app.</p><h3>Creating the OTP Component</h3><p>Now, let’s create a reusable OTP component that we can use in our application. Create a new file Otp.js in the src folder and paste the following code:</p><pre>// src/Otp.js<br><br>import React, { useEffect, useRef, useState } from &quot;react&quot;;<br><br>const otpCss = {<br>  width: &quot;40px&quot;,<br>  height: &quot;40px&quot;,<br>  margin: &quot;5px&quot;,<br>  textAlign: &quot;center&quot;,<br>  fontSize: &quot;1.2em&quot;,<br>};<br><br>const Otp = ({ length, onSubmit }) =&gt; {<br>  const inputRef = useRef([]);<br>  const [value, setValue] = useState(new Array(length).fill(&quot;&quot;));<br><br>  const onChangeHandler = ({ target: { value: inputValue } }, index) =&gt; {<br>    if (isNaN(inputValue)) return;<br><br>    const newValue = [...value];<br>    newValue[index] = inputValue.slice(-1);<br>    setValue(newValue);<br><br>    if (inputValue &amp;&amp; index &lt; length - 1) inputRef.current[index + 1].focus();<br><br>    const finalValue = newValue.join(&quot;&quot;);<br>    finalValue.length === length &amp;&amp; onSubmit(finalValue);<br>  };<br><br>  const onClickHandler = (index) =&gt; inputRef.current[index].setSelectionRange(1, 1);<br><br>  const onKeyDownHandler = (e, index) =&gt; {<br>    if (e.key === &quot;Backspace&quot; &amp;&amp; index &gt; 0 &amp;&amp; !value[index]) {<br>      inputRef.current[index - 1].focus();<br>    }<br>  };<br><br>  useEffect(() =&gt; {<br>    if (inputRef.current[0]) inputRef.current[0].focus();<br>  }, []);<br><br>  return (<br>    &lt;div&gt;<br>      &lt;h3&gt;Enter Your OTP&lt;/h3&gt;<br>      &lt;form&gt;<br>        {value.map((item, index) =&gt; (<br>          &lt;input<br>            key={index}<br>            ref={(input) =&gt; (inputRef.current[index] = input)}<br>            value={item}<br>            style={otpCss}<br>            placeholder=&quot;0&quot;<br>            onChange={(e) =&gt; onChangeHandler(e, index)}<br>            onClick={() =&gt; onClickHandler(index)}<br>            onKeyDown={(e) =&gt; onKeyDownHandler(e, index)}<br>          /&gt;<br>        ))}<br>      &lt;/form&gt;<br>    &lt;/div&gt;<br>  );<br>};<br>export default Otp;</pre><p>This component uses React hooks, refs, and useEffect to manage the state and behavior of the OTP input fields.</p><h3>Integrating the OTP Component</h3><p>Now, let’s integrate our Otp component into the main src/App.js file. Replace the contents of src/App.js with the following:</p><pre>// src/App.js<br><br>import React from &quot;react&quot;;<br>import Otp from &quot;./Otp&quot;;<br>import &quot;./App.css&quot;; // You can remove this if you don&#39;t have additional styling<br><br>function App() {<br><br>  const handleOtpSubmit = (otp) =&gt; {<br>    alert(`Submitted OTP: ${otp}`);<br>    // You can replace the alert with your authentication logic<br>  };<br><br>  return (<br>    &lt;div className=&quot;App&quot;&gt;<br>      &lt;Otp length={4} onSubmit={handleOtpSubmit} /&gt;<br>    &lt;/div&gt;<br>  );<br>}<br><br>export default App;</pre><p>This sets up a basic React application with the OTP component. Adjust the length prop based on your desired OTP length.</p><h3>Styling (Optional)</h3><p>Feel free to add additional styling to improve the visual appearance of your OTP screen. You can modify the otpCss object in the Otp.js file or create a separate CSS file for styling.</p><h3>Conclusion</h3><p>Congratulations! You’ve successfully created a basic OTP screen in React. This example provides a foundation that you can customize and extend to fit the requirements of your authentication flow. Experiment with different styling, add animations, or integrate it into a larger authentication system.</p><p>Remember to explore React’s documentation for further customization and improvement of your React applications.</p><p>Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=55281936b93d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Scroll-to-Top on Every Page Change in React]]></title>
            <link>https://medium.com/@narayan.k.dubey/scroll-to-top-on-every-page-change-in-react-e0894fcd8870?source=rss-eed3a16616cd------2</link>
            <guid isPermaLink="false">https://medium.com/p/e0894fcd8870</guid>
            <dc:creator><![CDATA[Narayan  Dubey]]></dc:creator>
            <pubDate>Thu, 20 Jul 2023 15:19:05 GMT</pubDate>
            <atom:updated>2023-07-20T15:31:14.093Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Introduction</strong></p><p>In the world of web development, seemingly simple tasks can sometimes lead to unexpected challenges. One such challenge is ensuring that the user’s scroll position resets to the top of the page every time they navigate to a new page. While it may appear trivial, getting this functionality right can significantly enhance user experience. In this blog, we will explore how to implement an elegant solution to the “<em>Scroll-to-Top</em>” problem in a React application. By following these steps, you’ll be able to provide your users with a seamless and intuitive browsing experience.</p><p><strong>Understanding the Scroll-to-Top Issue</strong></p><p>During the initial stages of a React project, you might notice that when you navigate between pages, the scroll position remains where it was on the previous page. This behavior is due to the way React handles page transitions. As the page changes, React preserves the current scroll position to give the impression of smooth navigation. However, this may not always be desired, especially when users expect the page to start from the top upon switching routes.</p><p><strong>The Solution: React Router and useEffect</strong></p><p>To overcome the scroll-to-top issue, we’ll be using React Router, the de facto routing solution for React applications. Additionally, we’ll employ the `<em>useEffect</em>` hook, a powerful feature in React that allows us to perform side effects after rendering components.</p><p><strong>Step 1:</strong> Install Dependencies</p><p>First, ensure you have React Router installed. If you don’t have it already, install it using npm or yarn:</p><pre><br>npm install react-router-dom</pre><p><strong>Step 2: Wrapping Your App with BrowserRouter</strong></p><p>In your main entry file, typically `<em>index.js</em>` or `<em>App.js</em>`, wrap your entire application with `<em>BrowserRouter</em>`. This provides the routing functionality for your app.</p><pre><br>import React from ‘react’;<br>import ReactDOM from ‘react-dom’;<br>import { BrowserRouter } from ‘react-router-dom’;<br>import App from ‘./App’; // Replace this with your main application component<br><br>ReactDOM.render(<br> &lt;BrowserRouter&gt;<br>   &lt;App /&gt;<br> &lt;/BrowserRouter&gt;,<br> document.getElementById(‘root’)<br>);</pre><p><strong>Step 3: Implementing the Scroll-to-Top Effect</strong></p><p>Now comes the crucial part: implementing the “<em>Scroll-to-Top</em>” effect using the `<em>useEffect</em>` hook. This will ensure that the page scrolls to the top every time the route changes.</p><pre><br>import React, { useEffect } from ‘react’;<br>import { useLocation } from ‘react-router-dom’;<br><br>const App = () =&gt; {<br> const location = useLocation();<br><br>useEffect(() =&gt; {<br>   // Scroll to the top of the page whenever the route changes<br>   window.scrollTo(0, 0);<br> }, [location]);<br><br>// … Rest of your component code …<br>};<br><br>export default App;</pre><p><strong>Explanation</strong></p><p>In this code, we use the `<em>useEffect</em>` hook to set up a side effect that will be executed whenever the `<em>location</em>` changes. The `<em>useLocation</em>` hook from React Router gives us access to the current location. When the route changes, the `<em>useEffect</em>` hook triggers, and `<em>window.scrollTo(0, 0)</em>` is called, which scrolls the page back to the top.</p><p><strong>Conclusion</strong></p><p>By following these steps, you’ve successfully implemented the “<em>Scroll-to-Top</em>” functionality in your React application. Now, whenever users navigate between pages, the page will automatically scroll to the top, providing a smoother and more intuitive browsing experience.</p><p>The process might seem simple once you understand it, but during the early stages of React development, overlooking this feature can lead to a less polished user experience. Mastering the “<em>Scroll-to-Top</em>” behavior will not only showcase your attention to detail as a developer but also contribute to a more enjoyable experience for your users. Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e0894fcd8870" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>