<?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 Surajan Shrestha on Medium]]></title>
        <description><![CDATA[Stories by Surajan Shrestha on Medium]]></description>
        <link>https://medium.com/@surajanshrestha?source=rss-cb1e45b8926e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*xM2hJTpn20aZmBuZkkP3fg.png</url>
            <title>Stories by Surajan Shrestha on Medium</title>
            <link>https://medium.com/@surajanshrestha?source=rss-cb1e45b8926e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 08:47:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@surajanshrestha/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[Bangalore: Things i LOVE, things i fucking DESPISE]]></title>
            <link>https://surajanshrestha.medium.com/bangalore-things-i-love-things-i-fucking-despise-328709e71c1b?source=rss-cb1e45b8926e------2</link>
            <guid isPermaLink="false">https://medium.com/p/328709e71c1b</guid>
            <category><![CDATA[change]]></category>
            <category><![CDATA[city-living]]></category>
            <category><![CDATA[traffic]]></category>
            <category><![CDATA[moving-on]]></category>
            <category><![CDATA[bangalore]]></category>
            <dc:creator><![CDATA[Surajan Shrestha]]></dc:creator>
            <pubDate>Mon, 17 Aug 2026 15:07:33 GMT</pubDate>
            <atom:updated>2026-08-17T15:07:33.633Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pdWZJrS9XRpz5blas8BDUw.jpeg" /><figcaption>I don’t wanna walk anymore (that’s my friend after a run)</figcaption></figure><p>Tl;dr love the weather, convenience, food, tech, work, office; fucking hate the traffic, honking, rash driving, and even more FUCKING HONKING!!!</p><p>For an open, technologically advanced city with Venture Capitalists, Entrepreneurs, Engineers, the goddamn fucking <strong>HONKING of HORNS</strong> all the time is a mismatch.</p><p>One night, I almost got hit by a guy on an Ather. Bro was on ~50km/h in a cramped residential area. He looked at me like it was my fault, I was literally walking.</p><p>It’s also crazy how you can own absoulte luxury like Porsche, BMWs, Range Rover, Mercedes and still drive like a drunk child with no license who stole the keys off of his father. I checked who the driver was though, it was an Uncle thrice my age, who looked like he was over-educated — like he had a Phd on something.</p><p>There’s also things I like. Like <strong>CONVENIENCE</strong>. My friend Zepto’d ice cream, chips and a summer BLANKET around midnight. It got delivered under 10 mins. I was like, “WOW”.</p><p>My friend literally used Blinkit to get a bunch of passport sized photos delivered under 10 mins. Again, “<strong>CONVENIENCE</strong>”.</p><p>Indiranagar is one of those places where you can bump into people from different backgrounds and half of them might be from Tech.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6Y3HcwEh1hP8duumwlZ1rQ.jpeg" /><figcaption>Semi Peaceful kinda…</figcaption></figure><p>Walking is fun with all the trees planted as if they knew how big Bangalore was gonna get, how much money was gonna get poured into it. BUT, it is only fun until a guy honks the most <strong>DIABOLICAL, LOUDEST, EARDRUM-DAMAGING</strong> horn inches away from you.</p><p>I know, I know, I’m complaining about horns too much but there is a thing about personal walking space. I’m really sorry if I’m sounding like an entitled spoiled brat, but you know, I know and everyone knows I’m speaking truth and everyone, especially an outsider feels this about Bangalore.</p><p>Enough bad things, let’s talk about WEATHER. I like, ahem, love the weather here. It gets sunny, then in a split second hazy. You’re walking and don’t even notice that it’s raining. It sprays some days when you’re walking and you only realise when your hair feels weirdly wet.</p><p>The other days, it POURS, but only for a few minutes, then it stops like it never happened painting the city streets wet like a full cleanse, like detoxed the city of all the dirt, shit and bad mistakes.</p><p>Oh yeah, almost forgot, u wanna know what I do here? I work at an AI Tech startup (duh) here in Indiranagar. I moved in to Bangalore from Kathmandu a few weeks ago. I have a few friends here — all of em — singular characters. Maybe I should write a novel on em, lol. They make me feel less lonely, I hope I did the same for them.</p><p>Office is pretty cool. We’re working on some hard problems. Lots of knowledge and skill gained every single day. All of the people in the office work pretty hard. Somedays we’re pulling 12–13 hr days. One thing I got to know was that if everyone is working hard, you also follow suit. Working late has never been more fun cuz you’re not doing it alone.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IMGx9150FM0EXJ3I4hXgpQ.jpeg" /><figcaption>Dogs, dogs and more dogs @ Cubbon Park</figcaption></figure><p>Sundays are for running and walking. My friends run but I walk — have a bad knee. Sundays are also for introspection. A 30 min walk around Cubban park with headphones is like a spiritual experience. Plus all the dogs there, half of em’ Golden Retrievers, pure joy.</p><p>I’m a transactional mf, so I plan to get more outta Bangalore than it gets outta me. All these things, the good, the best, the bad and the ugly will definitely leave a mark on me, I want it to, I wouldn’t want it any other way.</p><p>You reached the last paragraph, thanks for sticking along and spending you valuable time to read all of it. If, you’re a bot, then FUCK YOU.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=328709e71c1b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Only Thing That Matters Now Is TASTE]]></title>
            <link>https://surajanshrestha.medium.com/the-only-thing-that-matters-now-is-taste-6b88d76be296?source=rss-cb1e45b8926e------2</link>
            <guid isPermaLink="false">https://medium.com/p/6b88d76be296</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[startup]]></category>
            <category><![CDATA[indiedev]]></category>
            <category><![CDATA[vibe-coding]]></category>
            <category><![CDATA[apps]]></category>
            <dc:creator><![CDATA[Surajan Shrestha]]></dc:creator>
            <pubDate>Sat, 11 Apr 2026 16:31:54 GMT</pubDate>
            <atom:updated>2026-04-11T16:31:54.619Z</atom:updated>
            <content:encoded><![CDATA[<p><em>(and one uncomfortable truth that contradicts everything I just said)</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0ZfBt0NwanNUO0-2MbkgPg.png" /><figcaption>What do I mean by TASTE? Read the blog, it’s not that hard.</figcaption></figure><p>We are living in the weirdest era in the history of software.</p><p>A developer — solo, broke, sleep-deprived — can open a chat window, describe an app they’ve been sitting on for three years, and ship a production-ready MVP before the weekend ends.</p><p>No team. No runway. Just vibes, tokens, and a decent prompt.</p><p>This changes everything. And most people haven’t fully processed <em>how</em> much.</p><p>Because here’s the thing — when shipping becomes easy, <em>everyone</em> ships.</p><p>The flood gates don’t open slowly. They just… open.</p><p>And when everyone ships, you get what you’d expect: an ocean of apps competing for the same shrinking attention of the same finite number of humans.</p><p>There are only so many people on this planet. Only so many hours in a day. Only so many apps someone is willing to actually install, learn, and keep.</p><p>That number is <em>not</em> growing at the same speed as the apps being built.</p><p>So what separates the app that does $10M ARR from the one that quietly dies at $300 MRR?</p><p>It’s not the idea. Most good ideas are obvious — someone else has had them already.</p><p>It’s not the tech stack. Nobody cares if you’re on Next.js or Remix or whatever is trending this month.</p><p>It’s <strong>TASTE.</strong></p><p>Taste is that thing Steve Jobs had that most CEOs, most developers, most product managers — most <em>people</em> — simply don’t.</p><p>It’s not about making something pretty. It’s about knowing <em>exactly</em> when something feels off.</p><p>It’s that itch you can’t ignore when the button is 2px too low. When the copy sounds like a legal disclaimer. When the onboarding makes you feel stupid instead of capable.</p><p>Most people ship and move on. People with taste ship, stare at it, and fix the thing nobody asked them to fix.</p><p>Look at any competitive category — health apps, beauty, creativity tools, finance trackers.</p><p>Go download five of them in the same category. I dare you.</p><p>You won’t be able to tell me what’s different about each of them. Same features. Same promises. Same 4.2-star reviews with 600 ratings.</p><p>But one of them will feel <em>right</em>. Immediately. Without you being able to explain why.</p><p>That’s the one making the money.</p><p>Okay. Now for the uncomfortable part.</p><p>The title of this post says taste is the only thing that matters. And I mostly believe that.</p><p>But I’ve seen too much to say it’s the <em>complete</em> truth.</p><p><strong>Marketing is the other thing.</strong></p><p>Maybe even the more important thing — and I hate saying that.</p><p>I’ve used products that were genuinely bad. Slow, confusing, solving problems nobody had.</p><p>And they were <em>everywhere</em>. Ads. Influencers. Newsletter placements. TikTok reviews. And Memes, oh yes, MEMES.</p><p>And people used them. And some of those products made real money.</p><p>Meanwhile, a beautifully crafted app with zero distribution sat at 200 downloads with a 5-star average and a founder who couldn’t figure out why nobody cared.</p><p>The brutal reality: a bad product with great marketing beats a great product with no marketing.</p><p>Every time.</p><p>This isn’t cynical — it’s just how attention works. You can’t buy something you’ve never heard of. You can’t fall in love with an app you never downloaded.</p><p>So where does that leave us?</p><p>In a world where everyone can now build, the winners will be the ones who can do two things that have nothing to do with writing code:</p><p><strong>1. Feel what’s wrong before users can articulate it.</strong></p><p><strong>2. Make enough noise that the right people actually show up.</strong></p><p>Taste without reach is a private gallery.</p><p>Reach without taste is a billboard for something nobody wants to remember.</p><p>You need both. But if you can only pick one to obsess over first —</p><p>pick taste.</p><p>Because bad marketing for a great product can always be fixed with money.</p><p>Bad taste, once it ships? That’s a rebrand. And rebrands are funerals with confetti.</p><blockquote><em>Build something that feels like it was made by a person who gave a damn.</em></blockquote><blockquote><em>Then go tell people about it, loudly and repeatedly.</em></blockquote><blockquote><em>That’s the whole game now.</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6b88d76be296" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Monorepo in Next.js using Turborepo with Remote Caching & Vercel]]></title>
            <link>https://articles.readytowork.jp/monorepo-in-next-js-using-turborepo-with-remote-caching-vercel-bc30ebde8951?source=rss-cb1e45b8926e------2</link>
            <guid isPermaLink="false">https://medium.com/p/bc30ebde8951</guid>
            <category><![CDATA[remote-cache]]></category>
            <category><![CDATA[turborepo]]></category>
            <category><![CDATA[vercel]]></category>
            <category><![CDATA[monorepo]]></category>
            <category><![CDATA[nextjs]]></category>
            <dc:creator><![CDATA[Surajan Shrestha]]></dc:creator>
            <pubDate>Sun, 28 Apr 2024 11:26:16 GMT</pubDate>
            <atom:updated>2024-04-28T11:26:16.360Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SeLL03LESS4x2uFLOAsJ6Q.png" /><figcaption>Monorepo in Next.js using Turborepo with Remote Caching &amp; Vercel</figcaption></figure><p>Let’s setup a <strong>Next.js</strong> <strong>Monorepo </strong>project<strong> </strong>using the popular tool <strong>Turborepo</strong> by <strong>Vercel </strong>with additional features like <strong>Remote Caching</strong>.</p><p><a href="https://turbo.build/repo">Turborepo</a> is the go-to tool for creating a monorepo for a <a href="https://nextjs.org/">Next.js</a> project easily with minimal setup and fast deployment due to it’s remote caching feature.</p><p>As both <strong>Next.js</strong> and <strong>Turborepo</strong> are owned by <a href="https://vercel.com/"><strong>Vercel</strong></a>, it’s pretty easy to setup and scale accordingly. Let’s jump into it.</p><blockquote>Repo 👉: <a href="https://github.com/SurajanShrestha/rnd-turborepo">https://github.com/SurajanShrestha/rnd-turborepo</a></blockquote><h3>Tech Stack</h3><ol><li>Framework: <a href="https://nextjs.org/"><strong>Next.js</strong></a><strong> ⚡️</strong></li><li>Monorepo tool: <a href="https://turbo.build/repo"><strong>Turborepo</strong></a><strong> 🏎</strong></li><li>Deployment &amp; CI/CD: <a href="https://vercel.com/"><strong>Vercel</strong></a><strong> 🚀</strong></li><li>Package manager: <a href="https://www.npmjs.com/"><strong>npm</strong></a><strong> 🧳</strong></li></ol><h3>Things we’re doing</h3><p>This article contains the following topics <strong>(TL;DR)</strong>:</p><ol><li>What is a monorepo?</li><li>Why use a monorepo?</li><li>Why use Turborepo to setup a monorepo?</li><li>Setup a Next.js monorepo using Turborepo</li><li>Deploy to Vercel</li><li>Remote Caching</li></ol><h3>What is a monorepo?</h3><p>Monorepo is a <strong>single</strong> repository consisting of <strong>multiple</strong> projects with well-defined relationships. Each project/app has its own directory within the repository.</p><p><strong>Example:</strong> Consider someone developing a mobile app, a web app, and a backend API. These individual projects/apps can be stored and managed within a single Git repository.</p><h3>Why use a monorepo?</h3><p>The reasons to use a monorepo:</p><ol><li><strong>Shared code/components: </strong>Same components can be shared across multiple projects/apps within the same repo in a well-defined way.</li><li><strong>Easier dependency management: </strong>No need to worry about version conflicts between different libraries or frameworks. All the dependencies are stored in the same place, so they’re always compatible with each other.</li><li><strong>Simplified Build and Release Process (CI/CD)</strong>: With a monorepo, you can streamline the build and release process for multiple projects/apps within the same repo. It’s easier to coordinate and automate the build, testing, and deployment processes across the entire codebase.</li><li><strong>Unified Tooling and Workflows</strong>: Monorepo allows you to use a single set of tools, configurations, and workflows for the entire codebase. This simplifies the development process and ensures consistency across all the projects within the same repo.</li></ol><h3>Why use Turborepo to setup a Monorepo?</h3><p>You could use other tools like Lerna, Nx, etc. to setup a monorepo but I chose Turborepo for some of these reasons:</p><ol><li>Very little configuration needed to get started on a good monorepo.</li><li>If you’re using Next.js, then it works out of the box.</li><li>If you’re deploying with Vercel, it ships really well with little configuration (as <strong>Next.js</strong> and <strong>Turborepo</strong> both are owned by <strong>Vercel</strong>)</li><li>One of the main reasons is: <strong>REMOTE CACHING</strong>, which is a total gamechanger ⚡️. <br>Using Remote Caching with <strong>Vercel</strong>, you can save yourself a large chunk of build time when you are shipping changes frequently 🔥.</li></ol><h3>Setup a Next.js monorepo using Turborepo</h3><p>It’s pretty simple to setup a Next.js monorepo using Turborepo. Let’s begin:</p><h4>a. Create Turbo</h4><p>First, install create-turbo CLI, which will initialize a <strong>Next.js</strong> monorepo project:</p><pre>npx create-turbo@latest</pre><p>After this, you’ll be asked some questions:</p><p><strong>i. Where would you like to create your turborepo?<br></strong>👉 The default location is ./my-turborepo . If you select this, a new folder called <strong>“my-turborepo”</strong> will be created and the project will be initialized inside it.<br>But, i wanted it in my root directory so i did <strong>“dot(.)”</strong>: .</p><p><strong>ii. Which package manager do you want to use?<br>👉 </strong>You’ll get a bunch of options like: npm, pnpm, yarn, etc. <br>Let’s choose npm.</p><p>Then, Turborepo will generate a Next.js monorepo with a bunch of files. It will also install all the dependencies that the projects inside this monorepo depend upon.</p><h4>b. Explaining our new monorepo</h4><p>You’ll see the following folder structure in our new monorepo:</p><pre>-node_modules<br>-apps<br>  -docs<br>  -web<br>-packages<br>  -eslint-config<br>  -typescript-config<br>  -ui<br>-package.json<br>-turbo.json<br><strong><em>-Others...</em></strong></pre><p>All of these are called <strong>Workspaces</strong>. Let me explain:</p><p>i. <strong>apps/docs: </strong>A standalone Next.js with Typescript project/app.<strong><br></strong>ii. <strong>apps/web: </strong>A standalone Next.js with Typescript project/app.<strong><br></strong>iii. <strong>packages/ui: </strong>Shared UI components that can be used by any app i.e. apps/docs, apps/web, etc.<strong><br></strong>iv. <strong>packages/eslint-config: </strong>Shared Eslint configuration<strong><br></strong>v. <strong>packages/typescript-config: </strong>Shared Typescript configuration</p><h4>c. Explaining “package.json” of apps/web, apps/docs &amp; packages/ui</h4><p>You can see that inside dependencies in package.json of <strong>apps/web</strong> and <strong>apps/docs</strong>, it depends upon @repo/ui. In <strong>apps/web/package.json 👇</strong>:</p><pre>{<br>  &quot;name&quot;: &quot;web&quot;,<br>  &quot;dependencies&quot;: {<br>    &quot;@repo/ui&quot;: &quot;*&quot;<br>  }<br>  // Others<br>}</pre><p>Then, if we go to package.json of <strong>packages/ui, </strong>we’ll find that the name of the UI package is <strong>“@repo/ui”. </strong>In <strong>packages/ui/package.json</strong> 👇:</p><pre>{<br>  &quot;name&quot;: &quot;@repo/ui&quot;,<br>  // Others<br>}</pre><p>This means both of our apps depend upon <strong>packages/ui</strong> which has the package name of @repo/ui.</p><h4>d. Explaining imports &amp; exports</h4><p>Let’s see inside <strong>apps/web/app/page.tsx, </strong>the homepage for <strong>apps/web</strong>. We’ll see that it’s importing a Button component from @repo/ui/button.</p><p>We’ll see similar import in <strong>apps/docs/app/page.tsx</strong> also.</p><p>In <strong>apps/web/app/page.tsx 👇:</strong></p><pre>import { Button } from &quot;@repo/ui/button&quot;; 👈<br>// Other imports...<br> <br>export default function Page() {<br>  // Other code...<br>  return (<br>    &lt;main&gt;<br>      {/* Other code */}<br>      &lt;Button appName=&quot;web&quot; className={styles.button}&gt;<br>        Click me!<br>      &lt;/Button&gt;<br>    &lt;/main&gt;<br>  );<br>}</pre><p>Then, if you go to packages.json of <strong>package/ui</strong>, you’ll see the exports property. <br>The exports property tells the apps that are importing from <strong>package/ui</strong>, where to access the UI component from. <br><strong>For eg:</strong> To access the Button component, it must look inwards, inside the <strong>./src/button.tsx</strong> file.</p><p>In <strong>package/ui/package.json</strong> 👇:</p><pre>{<br>  &quot;name&quot;: &quot;@repo/ui&quot;,<br>  // Others<br>  &quot;exports&quot;: {<br>    &quot;./button&quot;: &quot;./src/button.tsx&quot;,<br>    &quot;./card&quot;: &quot;./src/card.tsx&quot;,<br>    &quot;./code&quot;: &quot;./src/code.tsx&quot;<br>  }<br>}</pre><p>But, this is kind of time consuming. What if we have a lot of components 🤔? It seems tedious to update package.json every single time 😵‍💫. Let’s change that.</p><h4>e. Modifying “exports” in packages/ui</h4><p>To solve the problem above 👆, let’s modify <strong>packages/ui</strong>. Inside <strong>packages/ui/src, </strong>create a new file called index.tsx. Using Atomic Design Principle, let’s export all of it’s sibling components: button.tsx, code.tsx, card.tsx, etc.</p><p>Inside <strong>packages/ui/src/index.tsx</strong> 👇:</p><pre>export * from &quot;./button&quot;;<br>export * from &quot;./card&quot;;<br>export * from &quot;./code&quot;;<br>// Export any other components you have...</pre><p>Then, in package.json of <strong>packages/ui</strong>, let’s update the exports property. Inside <strong>packages/ui/package.json</strong> 👇:</p><pre>{<br>  &quot;name&quot;: &quot;@repo/ui&quot;,<br>  // Others<br>  &quot;exports&quot;: {<br>    // Now, only one line will enable to export all of our components 😎<br>    &quot;./components&quot;: &quot;./src/index.tsx&quot;<br>  }<br>}</pre><p>Now, let’s fix our imports inside <strong>apps/web/app/page.tsx </strong>and <strong>apps/docs/app/page.tsx.<br></strong>Inside <strong>apps/web/app/page.tsx 👇 </strong>(Same thing for <strong>apps/docs</strong> application)<strong>:</strong></p><pre>// Importing Button, Card, Code components all in one single line<br>import { Button, Card, Code } from &quot;@repo/ui/components&quot;; 👈<br>// Other imports...<br> <br>export default function Page() {<br>  // Other code...<br>  return (<br>    &lt;main&gt;<br>      {/* Other code */}<br>      &lt;Button appName=&quot;web&quot; className={styles.button}&gt;<br>        Click me!<br>      &lt;/Button&gt;<br>    &lt;/main&gt;<br>  );<br>}</pre><h4>f. Explaining “turbo.json” and running commands</h4><p>In our monorepo’s root directory, you’ll find a file called turbo.json. This is an important file 🚨. This is how <strong>Turborepo</strong> manages tasks/commands for the whole monorepo.</p><p>Inside <strong>turbo.json</strong> 👇, you’ll a bunch of things:</p><pre>{<br>  &quot;$schema&quot;: &quot;https://turbo.build/schema.json&quot;,<br>  &quot;globalDependencies&quot;: [&quot;**/.env.*local&quot;],<br>  &quot;pipeline&quot;: {<br>    &quot;build&quot;: {<br>      &quot;dependsOn&quot;: [&quot;^build&quot;],<br>      &quot;outputs&quot;: [&quot;.next/**&quot;, &quot;!.next/cache/**&quot;]<br>    },<br>    &quot;lint&quot;: {<br>      &quot;dependsOn&quot;: [&quot;^lint&quot;]<br>    },<br>    &quot;dev&quot;: {<br>      &quot;cache&quot;: false,<br>      &quot;persistent&quot;: true<br>    }<br>  }<br>}</pre><p>Anything we put inside <strong>turbo.json</strong>’s pipeline property, will be a <strong>command</strong> that can be run.</p><p>You can run commands using turbo run &lt;task&gt; (or turbo &lt;task&gt; for short).</p><p>Here, we can run 3 commands: turbo run build, turbo run lint , turbo run dev OR in short form: turbo build, turbo lint , turbo dev .</p><p><strong>One way ☝️:</strong> To do so, you’ll need to install the turbo CLI globally:</p><pre>npm install turbo --global</pre><p><strong>Another way ✌️:</strong> But, there’s another way too. You could use the npx command (if you’re using npm) to use turbo CLI whenever you want without installing it globally. Like this: npx turbo run &lt;task&gt; (OR npx turbo &lt;task&gt; for short):</p><pre>npx turbo run &lt;task&gt;</pre><h4>g. Running apps/projects</h4><p>We currently have 2 projects: <strong>apps/docs</strong> and <strong>apps/web. </strong>We can either run each of them <strong>separately</strong> or run <strong>all of them at once</strong>.</p><p><strong>Running all projects at once 🗂:</strong> From root directory of our monorepo, run the command: npm run dev . It will run:</p><p>i. <strong>apps/docs</strong> in <strong>localhost:3001</strong><br>ii. <strong>apps/web</strong> in <strong>localhost:3000</strong></p><p><strong>Running one project at a time 📁:</strong> From root directory of our monorepo, run the command: npx turbo run dev --filter &lt;app_name&gt;. To run:</p><p>i. <strong>apps/docs</strong> use: npx turbo run dev --filter docs , will run in <strong>localhost:3001</strong><br>ii. <strong>apps/web</strong> use: npx turbo run dev --filter web , will run in <strong>localhost:3000</strong></p><h4>h. Push changes to Github</h4><p>Go ahead and push your changes to your Github repo. We’ll use that repo to deploy our projects later.</p><h3>Deploying to Vercel</h3><p>Let’s deploy our monorepo to <a href="https://vercel.com/"><strong>Vercel</strong></a><strong> 🚀</strong>. As our apps are in <strong>Next.js</strong>, created using <strong>Turborepo, </strong>deploying to <strong>Vercel</strong> is pretty straight-forward because all of these technologies are owned by <strong>Vercel</strong> itself.</p><p>The whole point of a monorepo is it’s flexibility in deploying each of the standalone apps inside it.</p><p>As we have two standalone apps: <strong>apps/docs</strong> &amp; <strong>apps/web,</strong> let’s deploy each of them as an individual project in Vercel. Both will have similar CI/CD setup with just some little differences.</p><h4>a. Deploying “apps/web”</h4><p>If you don’t have a Vercel account, you need to create one. I suggest signing up using your <a href="https://github.com/"><strong>Github</strong></a><strong> </strong>account<strong>. </strong>Follow these steps:</p><p><strong>i. Go to </strong><a href="https://vercel.com/"><strong>Vercel</strong></a><strong> &gt; Create or Add new project</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fEkf7JX2WwpiUCepCcp73g.png" /><figcaption>Create or Add a new Project on Vercel</figcaption></figure><p><strong>ii. Import your Git Repository<br></strong>Select your Github Repo and click on <strong>Import.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ja-Y_xn0zks2yATE9rKS_g.png" /><figcaption>Import your Git repo</figcaption></figure><p><strong>iii. Configure your project<br>This is how your configuration should look like. Don’t worry, it’s explained below 👇:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0w-Kvqo6XLwwVnr_4PAuGA.png" /><figcaption>Vercel Configuration for <strong>apps/web</strong></figcaption></figure><p>a. <strong>Give your “apps/web” project a name:</strong> I called mine rnd-turborepo.</p><p>b. <strong>Choose Framework:</strong> It should be, by default, selected to Next.js. If not, you can select it from a list of given options.</p><p>c. <strong>Select Root Directory:</strong> This is the directory, your project will run from. We’re deploying <strong>“apps/web”</strong>, so select apps/web.</p><p>d. <strong>Open </strong>Build and Output Settings<strong>:</strong><br>i. <strong>Build Command (IMPORTANT ⚠️):</strong> cd ../.. &amp;&amp; turbo run build --filter=web. <br>It means that, from inside <strong>apps/web, </strong>go to the root directory (cd ../..) and run turbo run build --filter=web , which will build <strong>only</strong> the <strong>apps/web </strong>project.<br>ii. <strong>Output Directory:</strong> Let it be default i.e. Next.js default<br>iii. <strong>Install Command: </strong>npm install</p><p>e. <strong>Hit </strong>Deploy<strong> 🚀: </strong>Your project will be deployed in a couple of seconds ✅. You can now view your project by clicking on one of the <strong>Domains</strong> link 👇.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RrnrTne1PNHxOSBbtYA1kw.png" /><figcaption>Successfully deployed <strong>apps/web </strong>on Vercel</figcaption></figure><p><strong>iv. [SUPER IMPORTANT ⚠️] Ignored Build Step<br></strong>After deployment, we have one last thing to do. For a monorepo, another really important step is setting up the<strong> Ignored Build Step</strong>.</p><p>Go to Settings tab &gt; Git menu &gt; Ignored Build Step section &gt; Select Custom behavior and put the command: npx turbo-ignore — fallback=HEAD^1 into it. Looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*73R0l-lpF_UW7cd6g2bd_A.png" /><figcaption>Ignored Build Step <strong>(Super Important)</strong></figcaption></figure><p><strong>What does it do 🤔?</strong><br>👉 It ensures that one <strong>app</strong> does not get built if changes are done only in <strong>another app.<br>For Eg: </strong>If changes are done only in <strong>apps/web</strong>, it makes no sense to automatically build <strong>apps/docs</strong> also 🙅‍♂️. So, we need to setup <strong>Ignored Build Step</strong> in each of our monorepo projects.</p><h4>b. Deploying “apps/docs”</h4><p>Follows the same thing as <strong>apps/web </strong>with only a few differences:</p><p><strong>i. Project name might be different:</strong> Mine is rnd-turborepo-docs <br><strong>ii. Root Directory:</strong> apps/docs <br><strong>iii. Build command will replace </strong>--filter=web<strong> with </strong>--filter=docs<strong>: </strong>cd ../.. &amp;&amp; turbo run build --filter=docs</p><blockquote>Same command for <strong>Ignored Build Step </strong>as given above in <strong>apps/web</strong>👆</blockquote><p>And that’s it for Deployment using <strong>Vercel</strong>. Let’s move on to <strong>Remote Caching</strong>.</p><h3>Remote Caching in Turborepo</h3><p>One of the most important and popular feature of Turborepo is <strong>Remote Caching</strong>. <strong>Remote Caching</strong> is the feature that allows you to share the cached outputs of tasks across your entire team and CI/CD pipeline.</p><p>This can significantly speed up builds by eliminating the need to re-run tasks that have already been completed by someone else on your team. This is especially useful for sharing <strong>build</strong> files with your team.</p><p><strong>Problem 😵‍💫:</strong> We know that <strong>building</strong> a Next.js app can take quite some time. With a monorepo, you have multiple Next.js apps, so <strong>building</strong> each of them might collectively take even more time.</p><p><strong>Solution ✅:</strong> What if, we could build our app with its latest changes, store that <strong>build cache</strong> somewhere and then reuse that <strong>cache </strong>later on? Sounds cool, doesn’t it?</p><p>We have deployed our projects <strong>(apps/web &amp; apps/docs)</strong> in Vercel. Let’s setup <strong>Remote Caching</strong>.</p><h4>a. Remote Caching on Vercel</h4><p>The pros of using <strong>Vercel</strong> for deployment is that when we use turbo as a build command in <strong>Vercel</strong>, it automatically creates <strong>remote cache</strong> when our project builds ✅. So, no need to reconfigure it.</p><p>Here, when we built our <strong>apps/web </strong>project, we can see that there was <strong>0 cache</strong> for the first time. Then, <strong>Vercel</strong> uploaded our build cache. We can now use this build cache in <strong>local machine</strong> too.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Nbxwu4HD5CDIK-Iwqmp24Q.png" /><figcaption>Build logs in Vercel with <strong>Remote Build Cache</strong></figcaption></figure><h4>b. Remote Caching on our Local Machine</h4><p>Now, we can build our apps in our local machine using the <strong>remote cache</strong> that was recently stored in <strong>Vercel</strong>.</p><p><strong>i. Turbo Login<br></strong>In our root directory, run the command: npx turbo login. This will authenticate the <strong>Turbo CLI</strong> with our <strong>Vercel</strong> account. <br>We’ll be redirected to Vercel’s authentication page, where we have to be logged in to allow authentication to our <strong>Turbo CLI.</strong></p><p><strong>ii. Link our Turborepo to Vercel’s Remote Cache<br></strong>Run the command: npx turbo link. This will link our repo in our <strong>local machine</strong> with the <strong>remote cache</strong> stored in <strong>Vercel.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TiTxx1vEqJQ2uzdIPTBs-g.png" /><figcaption>Link our <strong>Local Repo</strong> with Vercel’s <strong>Remote Cache</strong></figcaption></figure><p>You’ll be asked the following questions: <br><strong>- Question:</strong> Would you like to enable Remote Caching for &lt;Our Local Repo Location&gt;?<br><strong>-Answer 👉:</strong> y <br><strong>- Question:</strong> Which Vercel scope (and Remote Cache) do you want associated with this Turborepo?<br><strong>-Answer 👉:</strong> Select your <strong>Vercel</strong> account name/organization. (Sometimes we might be working under multiple <strong>organizations</strong> inside <strong>Vercel, </strong>so be sure to select that organization/account name where our project is deployed in).</p><p><strong>iii. Run local build &amp; see the magic<br></strong>Now, if you run npm run build, you’ll see that our whole monorepo will get built in <strong>under seconds or milliseconds (ms). </strong>When<strong> </strong>we<strong> </strong>see FULL TURBO, it means that our <strong>local build</strong> has fetched the <strong>remote cache</strong> stored in vercel. Now, that’s speed ⚡️.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8Z5R9nzvBKQyQTmBgHZBaw.png" /><figcaption>Run build in our <strong>local repo</strong>. It fetches <strong>Remote Cache</strong> from Vercel.</figcaption></figure><p>If we delete our local build cache and try again, it will still load the latest cache from <strong>Vercel</strong> 🔥. <br>From our root directory, we can delete our local build cache using the command:</p><pre>rm -rf ./node_modules/.cache/turbo</pre><p>Then, re run: npm run build. We’ll see that it still fetches from remote cache and goes FULL TURBO. That’s the power of Remote Caching.</p><p>Unless we change something in our local code and run <strong>build</strong>, it will load the <strong>remote build cache</strong> from <strong>Vercel</strong> based on the changes in each <strong>project/app</strong>.</p><p>If you’re at this section, then I want to thank you for giving my article your precious time and energy. If everything worked out for you, give this article a couple of <strong>claps 👏. <br></strong>If you have any queries, please feel free to comment below 💬.</p><blockquote><strong>Quote: “</strong>Luck is what happens when preparation meets opportunity.” — Seneca</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bc30ebde8951" width="1" height="1" alt=""><hr><p><a href="https://articles.readytowork.jp/monorepo-in-next-js-using-turborepo-with-remote-caching-vercel-bc30ebde8951">Monorepo in Next.js using Turborepo with Remote Caching &amp; Vercel</a> was originally published in <a href="https://articles.readytowork.jp/">readytowork, Inc.</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setup up Jest & React Testing Library with Pre-Commits & CircleCI]]></title>
            <link>https://articles.readytowork.jp/setup-up-jest-react-testing-library-with-pre-commits-circleci-2890d8453da1?source=rss-cb1e45b8926e------2</link>
            <guid isPermaLink="false">https://medium.com/p/2890d8453da1</guid>
            <category><![CDATA[jest]]></category>
            <category><![CDATA[circleci]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[pre-commit]]></category>
            <category><![CDATA[tdd]]></category>
            <dc:creator><![CDATA[Surajan Shrestha]]></dc:creator>
            <pubDate>Wed, 03 Apr 2024 22:16:58 GMT</pubDate>
            <atom:updated>2024-04-03T22:16:58.066Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M0JVavdvNoKp1vlOCeXB8Q.png" /></figure><p>Let’s set up a <strong>Jest</strong> &amp; <strong>React Testing Library</strong> with <strong>Pre-Commits</strong> (<strong>Husky</strong>) &amp; <strong>CI </strong>(<strong>CircleCI</strong>) for a proper Test Driven Development (TDD).</p><p>TDD involves not only writing tests and making sure our application runs as it’s expected but also making sure it ships 🚢 with our <strong>CI/CD</strong> pipelines &amp; has a good developer experience which can be provided using <strong>Pre-commit </strong>hooks.</p><h3><strong>Things we’re doing</strong></h3><p>This article is divided into two parts:</p><ol><li>Part One: Setting up TDD with Pre-Commits <strong>(Husky)</strong></li><li>Part Two: Setting up TDD with a CI/CD platform (<strong>CircleCI</strong>)</li></ol><blockquote>Repo 👉: <a href="https://github.com/SurajanShrestha/article-tdd-jest">https://github.com/SurajanShrestha/article-tdd-jest</a></blockquote><h3>Tech Stack</h3><ol><li>Setup tool: <a href="https://vitejs.dev/guide/"><strong>Vite</strong></a><strong> ⚡️ </strong>with react-ts as a React Typescript template.</li><li>Main packages: <a href="https://jestjs.io/"><strong>Jest</strong></a><strong> 🃏 </strong>&amp; <a href="https://testing-library.com/docs/react-testing-library/intro"><strong>React Testing Library</strong></a><strong> 🐙</strong></li><li>Pre-Commit tool: <a href="https://typicode.github.io/husky/"><strong>Husky</strong></a><strong> 🐶</strong></li><li>CI (Continuous Integration) platform: <a href="https://circleci.com/"><strong>CircleCI</strong></a><strong> 🚢</strong></li></ol><h3>Folder Structure</h3><p>This is the folder structure we’re going for:</p><pre><strong>-.circleci &lt;= CI setup using CircleCI</strong><br><strong>-.husky &lt;= Pre-Commit setup using Husky</strong><br>-src<br>  -components<br>    -Counter<br>      -index.tsx<br><strong>      -Counter.test.tsx &lt;= Test for Counter Component<br></strong>    -Link<br>      -index.tsx<br>      <strong>-Link.test.tsx &lt;= Test for Link Component</strong><br>      -__snapshots__ &lt;= Snapshot created by Snapshot test<br>  -<em>other stuff...</em><br>-package.json<br>-<em>node_modules &amp; other stuff...</em></pre><blockquote>Please ignore the test files (<strong>Counter.test.tsx</strong> &amp; <strong>Link.test.tsx</strong>)for now. I’ve written them just for the demo. They might not be perfect 😅.</blockquote><h3>1. Part One: Setup TDD with Pre-Commits (Husky)</h3><p>Pre-commit hooks are special scripts that run in <a href="https://git-scm.com/"><strong>Git</strong></a> before a commit is made. We use such hooks to allow commits to only happen when certain conditions are met. This promotes better code quality and reduces unnecessary commits.</p><p><a href="https://typicode.github.io/husky/"><strong>Husky</strong></a><strong> 🐶 </strong>is a go-to tool for handling &amp; setting up pre-commit hooks. This is how it works:</p><ol><li>When we try to commit, <strong>Husky</strong> triggers the pre-commit script.</li><li>The pre-commit script runs <strong>Jest</strong> to execute all our tests.</li><li>If all tests pass ✅, the commit proceeds normally.</li><li>If any tests fail ❌, <strong>Husky</strong> prevents the commit, and we’ll see error messages detailing the failures.</li></ol><p>This helps ensure code quality by catching errors before pushing changes.</p><h4>a. Install Husky</h4><p>Install <strong>husky </strong>using a package manager of your choice. I’m using <strong>npm. </strong>We’re installing Husky as a dev dependency.</p><pre>npm install --save-dev husky</pre><h4>b. Initialize Husky</h4><p>Run the husky init command on our root directory.</p><pre>npx husky init</pre><p>It does two things:</p><ol><li>Creates a pre-commit script inside the .husky folder like: .husky/pre-commit</li><li>Updates or adds prepare script in our <strong>package.json. </strong>Inside the <strong>scripts </strong>section in our package.json, we’ll find: “prepare”: “husky”</li></ol><h4>c. Setup test scripts in package.json</h4><p>In our <strong>package.json</strong>, in the <strong>“scripts”</strong> section let’s add our test scripts:</p><ol><li>“test”: “react-scripts test” : This is the default way to run tests. This will run our tests in watch mode 👀 i.e. if we change something in our code and hit <strong>save, all of our tests will re-run</strong>.</li><li>&quot;test:staged&quot;: &quot;CI=true react-scripts test --o&quot; : This runs tests in <strong>CI</strong> mode, which is more suitable to integrate with pre-commit hooks ✅.<br>The — o flag is used to run tests related with <strong>only those files that have changed since last commit 😎.</strong></li></ol><pre>// package.json<br>&quot;name&quot;: &quot;jest-react&quot;,<br>// ... usual stuff<br>&quot;scripts&quot;: {<br>  // ... usual stuff<br>  &quot;test&quot;: &quot;react-scripts test&quot;,<br>  &quot;test:staged&quot;: &quot;CI=true react-scripts test --o&quot;,<br>  &quot;prepare&quot;: &quot;husky&quot;<br>},</pre><h4>d. Setup tests to run before each commit</h4><p>In our ./husky/pre-commit file, write the command npm run test:staged which will run the “test:staged” script defined in our <strong>package.json</strong>.</p><pre>npm run test:staged</pre><p>If the tests pass ✅, the commit is allowed to proceed. Else, if the tests fail ❌, the commit is aborted and we’ll see error messages explaining why.</p><h4>d. Why did we use “test:staged” instead of “test” in our pre-commit 🤔?</h4><p>If we use “test”: “react-scripts test” inside our ./husky/pre-commit file, Jest will get stuck on <strong>watch mode ⚠️</strong>, whenever we add a new commit, regardless of if our tests had passed or failed. <br>So, this approach is not suitable for pre-commits ❌.</p><p>But, if we use “test:staged”: “CI=true react-scripts test --o”, Jest understands <strong>not</strong> to run tests in <strong>watch mode</strong> due to the CI=true command. The --o flag ensures that <strong>only files changed since the last commit</strong> <strong>are tested</strong>, speeding up development, particularly in large projects.<br>So, this approach is suitable for pre-commits ✅.</p><h4>e. Demo</h4><p>Let’s mess up our code knowingly so that some of our tests fail ❌.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7R6n-g6hFJcL2ei4GnS16A.png" /><figcaption>Changed code to make sure that tests fail ❌</figcaption></figure><p>Then, let’s add a new git commit git commit -m “testing husky”. We can see that our tests have failed ❌. Therefore, our commit does not get registered and gets discarded by Husky.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vwRlgmSssswGtyG4Cjl-kw.png" /><figcaption>Commit get’s discarded as Tests have failed ❌</figcaption></figure><p>Enough with failures, let’s try passing our tests ✅. Here, we’ve modified our code so that tests pass ✅.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5OqyPssKWhzkoXtn4Ksnkw.png" /><figcaption>Changed code to make sure tests pass ✅</figcaption></figure><p>Now, let’s add a new commit git commit -m “testing husky, tests should pass”. We can see that all of our tests pass ✅. Therefore, our commit is successfully registered ✅.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eKZpJZ419gD5zu3ZpAZqfw.png" /><figcaption>Commit is registered as Tests have passed ✅</figcaption></figure><h3>2. Part Two: Setup TDD with a CI/CD platform (<strong>CircleCI</strong>)</h3><p>We’ve successfully setup Jest with Pre-Commit. Now, we setup in a CI/CD platform. We’ve chosen <a href="https://circleci.com/"><strong>CircleCI</strong></a> for this.</p><p>When running tests with pre-commit, it’s better to <strong>run tests for only those files that have changed since last commit</strong>.</p><p>In a CI/CD environment 🚢, the <strong>best practice</strong> is to always <strong>run all tests</strong> so that our application is error prone ✅.</p><h4>a. Write a script to run all tests in CI mode</h4><p>In our <strong>package.json</strong>, let’s write another script that will run in CircleCI. The script is “test:staged_all”: “CI=true react-scripts test” .</p><p>This script “test:staged_all”, will run tests in CI mode (like we did in test:staged in Husky setup) but, it will <strong>run all the tests ⏳ </strong>rather than <strong>run tests for only those files that have changed since last commit</strong>.</p><pre>// package.json<br>&quot;name&quot;: &quot;jest-react&quot;,<br>// ... usual stuff<br>&quot;scripts&quot;: {<br>  // ... usual stuff<br>  &quot;test&quot;: &quot;react-scripts test&quot;,<br>  &quot;test:staged&quot;: &quot;CI=true react-scripts test --o&quot;,<br>  &quot;test:staged_all&quot;: &quot;CI=true react-scripts test&quot;,<br>  &quot;prepare&quot;: &quot;husky&quot;<br>},</pre><h4>b. Setup CircleCI Locally</h4><p>In our root directory, create a .circleci folder and inside it, create a config.yml file.</p><p>CircleCI generally creates a .circleci/config.yml file automatically when you create a project into a new git branch, but for the purpose of this article, we’re going to manually create it.</p><p>Put this into your .circleci/config.yml file:</p><pre># Couldn&#39;t automatically generate a config from your source code.<br># This is a generic template to serve as a base for your custom config<br><br># Use the latest 2.1 version of CircleCI pipeline process engine.<br># See: https://circleci.com/docs/configuration-reference<br>version: 2.1<br><br># Node.js Orb<br>orbs:<br>  node: circleci/node@5.0.2<br><br># Define a job to be invoked later in a workflow.<br># See: https://circleci.com/docs/jobs-steps/#jobs-overview &amp; https://circleci.com/docs/configuration-reference/#jobs<br>jobs:<br>  test:<br>    # Specify the execution environment. You can specify an image from Docker Hub or use one of our convenience images from CircleCI&#39;s Developer Hub.<br>    # See: https://circleci.com/docs/executor-intro/ &amp; https://circleci.com/docs/configuration-reference/#executor-job<br>    docker:<br>      # Specify the version you desire here<br>      # See: https://circleci.com/developer/images/image/cimg/base<br>      - image: cimg/base:current<br><br>    # Add steps to the job<br>    # See: https://circleci.com/docs/jobs-steps/#steps-overview &amp; https://circleci.com/docs/configuration-reference/#steps<br>    steps:<br>      # Checkout the code as the first step.<br>      - checkout<br>      # Install Node.js<br>      - node/install:<br>          node-version: &quot;16.13&quot;<br>      - run: node --version<br>      # Install Dependencies<br>      - run:<br>          name: Install dependencies<br>          command: npm install<br>      # Run Tests<br>      - run:<br>          name: Run tests<br>          command: npm run test:staged_all<br>  build:<br>    docker:<br>      - image: cimg/base:current<br>    steps:<br>      - checkout<br>      # Replace this with steps to build a package, or executable<br>      - run:<br>          name: Build an artifact<br>          command: touch example.txt<br>      - store_artifacts:<br>          path: example.txt<br>  deploy:<br>    docker:<br>      - image: cimg/base:current<br>    steps:<br>      # Replace this with steps to deploy to users<br>      - run:<br>          name: deploy<br>          command: &quot;#e.g. ./deploy.sh&quot;<br><br># Orchestrate jobs using workflows<br># See: https://circleci.com/docs/workflows/ &amp; https://circleci.com/docs/configuration-reference/#workflows<br>workflows:<br>  example:<br>    jobs:<br>      - test<br>      - build:<br>          requires:<br>            - test<br>      - deploy:<br>          requires:<br>            - test</pre><p>In line 25 of the config.yml file, under jobs:&gt;test:&gt;steps:, we have the following steps:</p><ol><li>node/install: : Installs node.js with node-version 16.13</li><li>run: : Checks if node.js is installed by running the command node --version</li><li>run: : Installs all dependencies related to our project with the npm install command.</li><li>run: : Run tests by running the command npm run test:staged_all</li></ol><p>You can ignore rest of the jibberish 🙇‍♂️.</p><h4>c. Push changes to Github</h4><p>CircleCI has been setup locally. Now, let’s <strong>commit</strong> and <strong>push</strong> our code to <strong>Github </strong>🌐.</p><h4>d. Signup with CircleCI</h4><p>Go to <a href="https://circleci.com/"><strong>CircleCI</strong></a>, and sign up for an account. If you already have one, then log into it.</p><h4>e. Create a new project</h4><p>In your CircleCI dashboard, go to <a href="https://app.circleci.com/projects">Projects</a> and click on “<strong>Create Project</strong>”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ueae9vib1YxSii9EnxEXjQ.png" /><figcaption>Create a new project in CircleCI</figcaption></figure><p>Now, select your remote repository service. My project repo is on <a href="https://github.com/SurajanShrestha/article-tdd-jest"><strong>Github</strong></a>, so i selected Github. You can choose between <strong>Github</strong>, <strong>Gitlab</strong> and <strong>Bitbucket</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Av4MgpfUox7Hea66kdLLKg.png" /><figcaption>Select your remote repository service</figcaption></figure><p>Confirm some details about your project. Give a <strong>Project Name, </strong>mine is article-tdd-jest. Follow the instructions to generate a <strong>Private SSH Key, </strong>use the <strong>public SSH key</strong> as a deploy key in our project’s repo in Github and copy the <strong>private key</strong> to add in the field given below <strong>(Image 1 🌁).</strong></p><p><strong>Alert ⚠️:</strong> Now, if you haven’t given CircleCI permissions to your project, you will not see your repository’s name in the <strong>Repository</strong> dropdown given below.</p><p><strong>Solution ✅:</strong> For that, just click on the link: <a href="https://github.com/settings/installations/47805402">Update your GitHub App repo permissions</a> and you’ll be redirected in CircleCI App in Github.<br>Go to <strong>Repository Access</strong> section, select “<strong>Only select repositories”</strong> &gt; select your repo that you need CircleCI to give access to. Mine’s article-tdd-jest <strong>(Image 2 🌁).</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*47uCtKiEQ6oAVFqLCrBMgg.png" /><figcaption><strong>Image 1:</strong> Confirm Details about your project in CircleCI</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oy3bHvPeYeOTXG_9zyR7vQ.png" /><figcaption><strong>Image 2:</strong> Give CircleCI access to your remote repository</figcaption></figure><p>Then, click on <strong>“Create Project”. </strong>CircleCI will create your project ✅.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rBz5EwpzmZ6PC0X2hAuLgg.png" /><figcaption>Create project on CircleCI</figcaption></figure><h4>f. See CircleCI in action</h4><p>Now, lets push some changes in Github to see CircleCI running its Continuous Integration (CI) pipeline automatically and also running our tests.</p><p>Lets update Counter.test.tsx so that it passes it’s tests ✅.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iYBgAzM5ix4QIRol4CJ5wA.png" /><figcaption>Updating <strong>Counter.test.tsx </strong>so that it passes it’s tests ✅</figcaption></figure><p>Then, <strong>commit</strong> and push into main branch in our Github.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O5TGdHCJlj6MECAHsWZPFQ.png" /><figcaption>Commit changes and push into <strong>main </strong>branch</figcaption></figure><p>See changes reflected on our Github. It should say <strong>“3 pending checks”</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n8hPQNl4dzwxIrC1pfpFjQ.png" /><figcaption>Check changes reflected on Github</figcaption></figure><p>See the CI pipeline in action in CircleCI. Click on the <strong>“test”</strong> job to see it in detail.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OQsY4LDTeGshNiov978q3g.png" /><figcaption>Click on “<strong>test” </strong>to see details</figcaption></figure><p>We can see all the actions running that we’d defined in our .circleci/config.yml file like: “<strong>Install Node.js 16.13”</strong>, <strong>“node — version”</strong>, <strong>“Install dependencies”</strong> and <strong>“Run tests”.</strong></p><p>Click on <strong>“Run Tests”</strong> to see tests in detail.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uJW_J2BgaI30ntWpDReA7Q.png" /><figcaption>See all the actions defined in our “<strong>.circleci/config.yml”</strong> file run</figcaption></figure><p>We can see that all of our tests (2 tests: <strong>Link.test.tsx</strong> &amp; <strong>Counter.test.tsx</strong>) ran and passed ✅.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LoBBnMJza3EsbUfrq2WICg.png" /><figcaption>See test details by clicking on <strong>“Run tests”</strong></figcaption></figure><p>Thank you for coming this far 🙇‍♂️. I’ll forever be grateful for giving my article your valuable time and energy. Be sure to leave claps 👏 and comment if you have any queries.</p><p>Happy Coding!</p><blockquote>Quote: “If you are not willing to be a fool, you can’t become a master.” — Jordan B. Peterson</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2890d8453da1" width="1" height="1" alt=""><hr><p><a href="https://articles.readytowork.jp/setup-up-jest-react-testing-library-with-pre-commits-circleci-2890d8453da1">Setup up Jest &amp; React Testing Library with Pre-Commits &amp; CircleCI</a> was originally published in <a href="https://articles.readytowork.jp/">readytowork, Inc.</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implementing a QR Code Scanner in React]]></title>
            <link>https://articles.readytowork.jp/implementing-a-qr-code-scanner-in-react-4c8f4e3c6f2e?source=rss-cb1e45b8926e------2</link>
            <guid isPermaLink="false">https://medium.com/p/4c8f4e3c6f2e</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[qr-scanner]]></category>
            <category><![CDATA[qr-code]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Surajan Shrestha]]></dc:creator>
            <pubDate>Sun, 07 Jan 2024 09:36:40 GMT</pubDate>
            <atom:updated>2026-04-11T17:21:54.149Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ALM3oGvY9VHF5a5JwwTbSQ.png" /><figcaption>QR Code Scanner in React</figcaption></figure><p><strong>QR Code Scanners</strong> are extremely popular and here’s how to implement it in <a href="https://react.dev/">React</a>. It is simple enough to implement but if building from scratch, it might take your whole day.</p><p>Luckily, I’m here to help you out 🙌.</p><p><strong>Problem 😵‍💫:</strong> Although we have a lot of <strong>plug-in-play</strong> types of QR Code Scanner packages dedicated to React, many are <strong>not that stable</strong>, some have <strong>bugs</strong> specific to a certain browser, some don’t have enough <strong>flexibility</strong> to use and some just lack certain <strong>features</strong>.</p><p><strong>Solution ✅:</strong> I found out that QR Code Scanner packages that have a direct implementation with JavaScript, rather than specialize with a certain JS Framework, are much better and stable.</p><h4><strong>Package Used</strong></h4><p>I’ll be using the <a href="https://www.npmjs.com/package/qr-scanner">qr-scanner</a> package which is <strong>pretty stable</strong> and is sponsored by <strong>nimiq</strong> (which is a browser-based blockchain and i guess they use this package in their application too).</p><blockquote>Repo Link 👉: <a href="https://github.com/SurajanShrestha/qr-scanner-in-react">https://github.com/SurajanShrestha/qr-scanner-in-react</a></blockquote><h4>Let’s Code</h4><p><strong>a. Folder Structure</strong></p><p>I’m using <a href="https://vitejs.dev/">vite</a> to setup development environment. But, you can use anything of your choice, even <strong>create-react-app</strong> 😉.</p><pre>-node_modules and others<br>-package.json<br>-src<br>  -assets<br>    -qr-frame.svg<br>  -components<br>    -QrReader.tsx<br>    -QrStyles.css<br>  -App.tsx<br>  -main.tsx<strong>(or index.tsx if you&#39;re using create-react-app)</strong><br>  -App.css<strong>(this is an empty file)</strong><br>  -index.css<strong>(this is an empty file)</strong></pre><p><strong>b. Install Package</strong></p><p>For npm:</p><pre>npm i qr-scanner</pre><p>For yarn:</p><pre>yarn add qr-scanner</pre><p><strong>c. Clean Unwanted CSS</strong></p><p>I’ve deleted all the css from App.css and index.css so that i’ll have a clean slate to work on and also so that css styles don’t conflict each other.</p><p>Removing styles in bothApp.css and index.css👇:</p><pre>/* No CSS Styles here */</pre><p>For the purpose of simplifying css styles, we’ll only use one css file located in /components/QrStyles.css. This is all the css styles we’ll need for our QR Scanner👇:</p><pre>.qr-reader {<br>  width: 430px;<br>  height: 100vh;<br>  margin: 0 auto;<br>  position: relative;<br>}<br><br>.qr-reader video {<br>  width: 100%;<br>  height: 100%;<br>  object-fit: cover;<br>}<br><br>.qr-reader .qr-box {<br>  width: 100% !important;<br>  left: 0 !important;<br>}<br><br>.qr-reader .qr-frame {<br>  position: absolute;<br>  fill: none;<br>  left: 50%;<br>  top: 50%;<br>  transform: translateX(-50%) translateY(-50%);<br>}<br><br>/* Media Queries for mobile screens */<br>@media (max-width: 426px) {<br>  .qr-reader {<br>    width: 100%;<br>  }<br>}</pre><p><strong>d. Import Dependencies</strong></p><p>Let’s make a <strong>QrReader</strong> component in the following route /components/QrReader.tsx and import <strong>qr-scanner, QrFrame and QrStyles.css:</strong></p><pre>// Styles<br>import &quot;./QrStyles.css&quot;;<br><br>// Qr Scanner<br>import QrScanner from &quot;qr-scanner&quot;;<br>import QrFrame from &quot;../assets/qr-frame.svg&quot;;<br><br>const QrReader = () =&gt; {<br><br>}<br><br>export default QrReader;</pre><p><strong>e. Mount JSX Elements needed by our Qr Scanner</strong></p><p>We’ll mainly be needing a &lt;video&gt; element where the camera renders and an &lt;img&gt; element to show our QrFrame.svg.</p><p>We’ll attach the videoEl and qrBoxEl that we got from <strong>useRef</strong> into their respective elements.</p><p>Apart from that we’ll be needing a bunch of &lt;div&gt; elements in the exact order and having the exact css classes as given below 👇:</p><pre>import { useEffect, useRef, useState } from &quot;react&quot;;<br><br>// Styles<br>import &quot;./QrStyles.css&quot;;<br><br>// Qr Scanner<br>import QrScanner from &quot;qr-scanner&quot;;<br>import QrFrame from &quot;../assets/qr-frame.svg&quot;;<br><br>const QrReader = () =&gt; {<br>  // QR States<br>  const videoEl = useRef&lt;HTMLVideoElement&gt;(null);<br>  const qrBoxEl = useRef&lt;HTMLDivElement&gt;(null);<br><br>  return (<br>    &lt;div className=&quot;qr-reader&quot;&gt;<br>      {/* QR */}<br>      &lt;video ref={videoEl}&gt;&lt;/video&gt;<br>      &lt;div ref={qrBoxEl} className=&quot;qr-box&quot;&gt;<br>        &lt;img<br>          src={QrFrame}<br>          alt=&quot;Qr Frame&quot;<br>          width={256}<br>          height={256}<br>          className=&quot;qr-frame&quot;<br>        /&gt;<br>      &lt;/div&gt;<br>    &lt;/div&gt;<br>  );<br>}<br><br>export default QrReader;</pre><p><strong>f. Initialize QR Scanner</strong></p><p>For initializing our Qr Scanner, we’ll need a scanner element from <strong>useRef</strong> and qrOn state &amp; setQrOn state handler.</p><p>We’ll initialize the Qr Scanner in our <strong>useEffect</strong>. To do so, we’ll use the constructor function given by <strong>qr-scanner </strong>package i.e. new QrScanner() .</p><p>The 1st parameter in new QrScanner() is videoEl?.current where our camera renders, 2nd is onScanSuccess (we’ll get to that in next section) and 3rd is the options object containing properties like: onDecodeError, preferredCamera, highlightScanRegion, highlightCodeOutline and overlay . What each of these options do, is provided in the comments inside the code below 👇.</p><p>We start our Qr Scanner, using the scanner?.current promise function. It will either set qrOn state to <strong>true, </strong>if qr scanner is started successfully ✅, or will set it to <strong>false</strong>, if there was a problem in starting the scanner ❌.</p><p>We’ll stop our Qr Scanner when user goes to another page or our QrReader component <strong>unmounts</strong> (is removed from JavaScript’s DOM tree). When Qr Scanner is stopped, it will stop using the device’s camera 🎬. We’ll do it using the scanner?.current?.stop() function inside our <strong>useEffect’s cleanup function</strong>.</p><pre>import { useEffect, useRef, useState } from &quot;react&quot;;<br><br>// Styles<br>import &quot;./QrStyles.css&quot;;<br><br>// Qr Scanner<br>import QrScanner from &quot;qr-scanner&quot;;<br>import QrFrame from &quot;../assets/qr-frame.svg&quot;;<br><br>const QrReader = () =&gt; {<br>  // QR States<br>  const scanner = useRef&lt;QrScanner&gt;();<br>  const videoEl = useRef&lt;HTMLVideoElement&gt;(null);<br>  const qrBoxEl = useRef&lt;HTMLDivElement&gt;(null);<br>  const [qrOn, setQrOn] = useState&lt;boolean&gt;(true);<br><br>  useEffect(() =&gt; {<br>    if (videoEl?.current &amp;&amp; !scanner.current) {<br>      // 👉 Instantiate the QR Scanner<br>      scanner.current = new QrScanner(videoEl?.current, onScanSuccess, {<br>        onDecodeError: onScanFail,<br>        // 📷 This is the camera facing mode. In mobile devices, &quot;environment&quot; means back camera and &quot;user&quot; means front camera.<br>        preferredCamera: &quot;environment&quot;,<br>        // 🖼 This will help us position our &quot;QrFrame.svg&quot; so that user can only scan when qr code is put in between our QrFrame.svg.<br>        highlightScanRegion: true,<br>        // 🔥 This will produce a yellow (default color) outline around the qr code that we scan, showing a proof that our qr-scanner is scanning that qr code.<br>        highlightCodeOutline: true,<br>        // 📦 A custom div which will pair with &quot;highlightScanRegion&quot; option above 👆. This gives us full control over our scan region.<br>        overlay: qrBoxEl?.current || undefined,<br>      });<br><br>      // 🚀 Start QR Scanner<br>      scanner?.current<br>        ?.start()<br>        .then(() =&gt; setQrOn(true))<br>        .catch((err) =&gt; {<br>          if (err) setQrOn(false);<br>        });<br>    }<br><br>    // 🧹 Clean up on unmount.<br>    // 🚨 This removes the QR Scanner from rendering and using camera when it is closed or removed from the UI.<br>    return () =&gt; {<br>      if (!videoEl?.current) {<br>        scanner?.current?.stop();<br>      }<br>    };<br>  }, []);<br><br>  return (<br>    &lt;div className=&quot;qr-reader&quot;&gt;<br>      {/* QR */}<br>      &lt;video ref={videoEl}&gt;&lt;/video&gt;<br>      &lt;div ref={qrBoxEl} className=&quot;qr-box&quot;&gt;<br>        &lt;img<br>          src={QrFrame}<br>          alt=&quot;Qr Frame&quot;<br>          width={256}<br>          height={256}<br>          className=&quot;qr-frame&quot;<br>        /&gt;<br>      &lt;/div&gt;<br>    &lt;/div&gt;<br>  );<br>}<br><br>export default QrReader;</pre><p><strong>e. Handle Scanned Result and Failures</strong></p><p>We’ll handle our scanned result using the onScanSuccess function which is passed as a 2nd parameter in the new Scanner() constructor function. When our Qr Scanner has successfully extracted the data from given Qr Code, it is passed in the result parameter in onScanSuccess function.</p><p>We’ll console.log the result (for debugging purposes) and also set the scannedResult state with our result’s data ✅. We’ll then show the scannedResult as an absolutely positioned &lt;p&gt; element that hovers on top of our camera view.</p><p>For handling failures, we’ll use the onScanFail function where we console.log our <strong>error ❌</strong>. The onScanFail function is passed inside the 3rd parameter object’s 1st property in new Scanner() the constructor function.</p><pre>import { useEffect, useRef, useState } from &quot;react&quot;;<br><br>// Styles<br>import &quot;./QrStyles.css&quot;;<br><br>// Qr Scanner<br>import QrScanner from &quot;qr-scanner&quot;;<br>import QrFrame from &quot;../assets/qr-frame.svg&quot;;<br><br>const QrReader = () =&gt; {<br>  // QR States<br>  const scanner = useRef&lt;QrScanner&gt;();<br>  const videoEl = useRef&lt;HTMLVideoElement&gt;(null);<br>  const qrBoxEl = useRef&lt;HTMLDivElement&gt;(null);<br>  const [qrOn, setQrOn] = useState&lt;boolean&gt;(true);<br><br>  // Result<br>  const [scannedResult, setScannedResult] = useState&lt;string | undefined&gt;(&quot;&quot;);<br><br>  // Success<br>  const onScanSuccess = (result: QrScanner.ScanResult) =&gt; {<br>    // 🖨 Print the &quot;result&quot; to browser console.<br>    console.log(result);<br>    // ✅ Handle success.<br>    // 😎 You can do whatever you want with the scanned result.<br>    setScannedResult(result?.data);<br>  };<br><br>  // Fail<br>  const onScanFail = (err: string | Error) =&gt; {<br>    // 🖨 Print the &quot;err&quot; to browser console.<br>    console.log(err);<br>  };<br><br>  useEffect(() =&gt; {<br>    if (videoEl?.current &amp;&amp; !scanner.current) {<br>      // 👉 Instantiate the QR Scanner<br>      scanner.current = new QrScanner(videoEl?.current, onScanSuccess, {<br>        onDecodeError: onScanFail,<br>        // 📷 This is the camera facing mode. In mobile devices, &quot;environment&quot; means back camera and &quot;user&quot; means front camera.<br>        preferredCamera: &quot;environment&quot;,<br>        // 🖼 This will help us position our &quot;QrFrame.svg&quot; so that user can only scan when qr code is put in between our QrFrame.svg.<br>        highlightScanRegion: true,<br>        // 🔥 This will produce a yellow (default color) outline around the qr code that we scan, showing a proof that our qr-scanner is scanning that qr code.<br>        highlightCodeOutline: true,<br>        // 📦 A custom div which will pair with &quot;highlightScanRegion&quot; option above 👆. This gives us full control over our scan region.<br>        overlay: qrBoxEl?.current || undefined,<br>      });<br><br>      // 🚀 Start QR Scanner<br>      scanner?.current<br>        ?.start()<br>        .then(() =&gt; setQrOn(true))<br>        .catch((err) =&gt; {<br>          if (err) setQrOn(false);<br>        });<br>    }<br><br>    // 🧹 Clean up on unmount.<br>    // 🚨 This removes the QR Scanner from rendering and using camera when it is closed or removed from the UI.<br>    return () =&gt; {<br>      if (!videoEl?.current) {<br>        scanner?.current?.stop();<br>      }<br>    };<br>  }, []);<br><br>  return (<br>    &lt;div className=&quot;qr-reader&quot;&gt;<br>      {/* QR */}<br>      &lt;video ref={videoEl}&gt;&lt;/video&gt;<br>      &lt;div ref={qrBoxEl} className=&quot;qr-box&quot;&gt;<br>        &lt;img<br>          src={QrFrame}<br>          alt=&quot;Qr Frame&quot;<br>          width={256}<br>          height={256}<br>          className=&quot;qr-frame&quot;<br>        /&gt;<br>      &lt;/div&gt;<br><br>      {/* Show Data Result if scan is success */}<br>      {scannedResult &amp;&amp; (<br>        &lt;p<br>          style={{<br>            position: &quot;absolute&quot;,<br>            top: 0,<br>            left: 0,<br>            zIndex: 99999,<br>            color: &quot;white&quot;,<br>          }}<br>        &gt;<br>          Scanned Result: {scannedResult}<br>        &lt;/p&gt;<br>      )}<br>    &lt;/div&gt;<br>  );<br>};<br><br>export default QrReader;</pre><p><strong>f. The Full Code 👇 <br><em>(And, handling errors when Camera is not allowed in Browser Permissions)</em></strong></p><p>If user has blocked camera permissions in their browser, we’ll show a <strong>JavaScript alert </strong>using the alert() function 😎.</p><p>The Final Code is given below 👇:</p><pre>import { useEffect, useRef, useState } from &quot;react&quot;;<br><br>// Styles<br>import &quot;./QrStyles.css&quot;;<br><br>// Qr Scanner<br>import QrScanner from &quot;qr-scanner&quot;;<br>import QrFrame from &quot;../assets/qr-frame.svg&quot;;<br><br>const QrReader = () =&gt; {<br>  // QR States<br>  const scanner = useRef&lt;QrScanner&gt;();<br>  const videoEl = useRef&lt;HTMLVideoElement&gt;(null);<br>  const qrBoxEl = useRef&lt;HTMLDivElement&gt;(null);<br>  const [qrOn, setQrOn] = useState&lt;boolean&gt;(true);<br><br>  // Result<br>  const [scannedResult, setScannedResult] = useState&lt;string | undefined&gt;(&quot;&quot;);<br><br>  // Success<br>  const onScanSuccess = (result: QrScanner.ScanResult) =&gt; {<br>    // 🖨 Print the &quot;result&quot; to browser console.<br>    console.log(result);<br>    // ✅ Handle success.<br>    // 😎 You can do whatever you want with the scanned result.<br>    setScannedResult(result?.data);<br>  };<br><br>  // Fail<br>  const onScanFail = (err: string | Error) =&gt; {<br>    // 🖨 Print the &quot;err&quot; to browser console.<br>    console.log(err);<br>  };<br><br>  useEffect(() =&gt; {<br>    if (videoEl?.current &amp;&amp; !scanner.current) {<br>      // 👉 Instantiate the QR Scanner<br>      scanner.current = new QrScanner(videoEl?.current, onScanSuccess, {<br>        onDecodeError: onScanFail,<br>        // 📷 This is the camera facing mode. In mobile devices, &quot;environment&quot; means back camera and &quot;user&quot; means front camera.<br>        preferredCamera: &quot;environment&quot;,<br>        // 🖼 This will help us position our &quot;QrFrame.svg&quot; so that user can only scan when qr code is put in between our QrFrame.svg.<br>        highlightScanRegion: true,<br>        // 🔥 This will produce a yellow (default color) outline around the qr code that we scan, showing a proof that our qr-scanner is scanning that qr code.<br>        highlightCodeOutline: true,<br>        // 📦 A custom div which will pair with &quot;highlightScanRegion&quot; option above 👆. This gives us full control over our scan region.<br>        overlay: qrBoxEl?.current || undefined,<br>      });<br><br>      // 🚀 Start QR Scanner<br>      scanner?.current<br>        ?.start()<br>        .then(() =&gt; setQrOn(true))<br>        .catch((err) =&gt; {<br>          if (err) setQrOn(false);<br>        });<br>    }<br><br>    // 🧹 Clean up on unmount.<br>    // 🚨 This removes the QR Scanner from rendering and using camera when it is closed or removed from the UI.<br>    return () =&gt; {<br>      if (!videoEl?.current) {<br>        scanner?.current?.stop();<br>      }<br>    };<br>  }, []);<br><br>  // ❌ If &quot;camera&quot; is not allowed in browser permissions, show an alert.<br>  useEffect(() =&gt; {<br>    if (!qrOn)<br>      alert(<br>        &quot;Camera is blocked or not accessible. Please allow camera in your browser permissions and Reload.&quot;<br>      );<br>  }, [qrOn]);<br><br>  return (<br>    &lt;div className=&quot;qr-reader&quot;&gt;<br>      {/* QR */}<br>      &lt;video ref={videoEl}&gt;&lt;/video&gt;<br>      &lt;div ref={qrBoxEl} className=&quot;qr-box&quot;&gt;<br>        &lt;img<br>          src={QrFrame}<br>          alt=&quot;Qr Frame&quot;<br>          width={256}<br>          height={256}<br>          className=&quot;qr-frame&quot;<br>        /&gt;<br>      &lt;/div&gt;<br><br>      {/* Show Data Result if scan is success */}<br>      {scannedResult &amp;&amp; (<br>        &lt;p<br>          style={{<br>            position: &quot;absolute&quot;,<br>            top: 0,<br>            left: 0,<br>            zIndex: 99999,<br>            color: &quot;white&quot;,<br>          }}<br>        &gt;<br>          Scanned Result: {scannedResult}<br>        &lt;/p&gt;<br>      )}<br>    &lt;/div&gt;<br>  );<br>};<br><br>export default QrReader;</pre><h4>Results</h4><p>For the purpose of seeing if our QR Scanner works, i’ve used a QR Code which when scanned should extract my Github profile’s link 👇: <a href="https://github.com/SurajanShrestha">https://github.com/SurajanShrestha</a></p><p><strong>a. On a Desktop/Laptop Browser 💻</strong></p><p>On Success ✅:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7VEZMUxZliEQMeziiKcnUg.png" /><figcaption>QR Scan Success</figcaption></figure><p>When the camera is not allowed in Browser Permissions ❌:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lnGNu47e4pk4XhaFM6JGkQ.png" /><figcaption>Show alert when camera is not allowed in Browser Permissions</figcaption></figure><p><strong>b. On a Mobile Browser 📱</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/288/1*pCz4Ws5ghiMoITubsw9AZA.gif" /><figcaption>QR Scan in Mobile Phone</figcaption></figure><p>If you’re at this section i.e. the end, I want you to know that I’m forever grateful for giving my article your valuable time and energy. If this article was helpful to you, please feel free to clap for it 👏.</p><p>Again, thank you, and keep coding 😎.</p><blockquote>Quote: Every man has two lives. The second one begins, when he realizes that he only has one.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4c8f4e3c6f2e" width="1" height="1" alt=""><hr><p><a href="https://articles.readytowork.jp/implementing-a-qr-code-scanner-in-react-4c8f4e3c6f2e">Implementing a QR Code Scanner in React</a> was originally published in <a href="https://articles.readytowork.jp/">readytowork, Inc.</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Defer, Panic, and Recover in Go for Error Handling]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://articles.readytowork.jp/defer-panic-and-recover-in-go-for-error-handling-ca34792f00d8?source=rss-cb1e45b8926e------2"><img src="https://cdn-images-1.medium.com/max/1600/1*cdVhzyCQQ1GJ7Ai_xgSOlw.png" width="1600"></a></p><p class="medium-feed-snippet">Error Handling is one of the most important topics to consider while building software. In this article, we will learn about how to&#x2026;</p><p class="medium-feed-link"><a href="https://articles.readytowork.jp/defer-panic-and-recover-in-go-for-error-handling-ca34792f00d8?source=rss-cb1e45b8926e------2">Continue reading on readytowork, Inc. »</a></p></div>]]></description>
            <link>https://articles.readytowork.jp/defer-panic-and-recover-in-go-for-error-handling-ca34792f00d8?source=rss-cb1e45b8926e------2</link>
            <guid isPermaLink="false">https://medium.com/p/ca34792f00d8</guid>
            <category><![CDATA[go]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[error-handling]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Surajan Shrestha]]></dc:creator>
            <pubDate>Mon, 25 Sep 2023 00:52:37 GMT</pubDate>
            <atom:updated>2023-09-25T00:52:37.464Z</atom:updated>
        </item>
    </channel>
</rss>