<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tushar Tushar</title>
    <description>The latest articles on DEV Community by Tushar Tushar (@tushar_sandhu).</description>
    <link>https://dev.to/tushar_sandhu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2583604%2Fcc8590eb-a61d-4d7d-86d0-d7ce610843fa.png</url>
      <title>DEV Community: Tushar Tushar</title>
      <link>https://dev.to/tushar_sandhu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tushar_sandhu"/>
    <language>en</language>
    <item>
      <title>Using Dnd-Kit Basics</title>
      <dc:creator>Tushar Tushar</dc:creator>
      <pubDate>Sat, 31 May 2025 18:58:47 +0000</pubDate>
      <link>https://dev.to/tushar_sandhu/using-dnd-kit-basics-1hla</link>
      <guid>https://dev.to/tushar_sandhu/using-dnd-kit-basics-1hla</guid>
      <description>&lt;p&gt;if you want to create draggable/droppable elements on your website one of the options available is &lt;a href="https://dndkit.com/" rel="noopener noreferrer"&gt;Dnd-kit&lt;/a&gt; which is a modern drag-and-drop toolkit for React. Using it is fairly very easy, unless you end up on youtube where for some reason every one wants to make &lt;em&gt;kanban&lt;/em&gt; boards with it and suddenly instead of learning how to use Dnd-kit now you are also dealing with the added complexity of a &lt;em&gt;kanban board&lt;/em&gt;. So, I am just going to do a very simple and straightforward setup for Dnd-kit just going over how to use it and what we are doing.&lt;/p&gt;

&lt;p&gt;Don't want to read just want the code? Here is the 🔗 &lt;a href="https://github.com/tushar-droid/Dnd-kit-Tutorial.git" rel="noopener noreferrer"&gt;git link&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's Breakdown our steps
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;🛠️ Create a React Project with Vite. &lt;/li&gt;
&lt;li&gt;🪄 Adding Dnd-context&lt;/li&gt;
&lt;li&gt;👀 Do some very Basic page setup like giving the page full height/width etc.&lt;/li&gt;
&lt;li&gt;🖱️ Making a Draggable Item.&lt;/li&gt;
&lt;li&gt;📦 Making a Droppable Item.&lt;/li&gt;
&lt;li&gt;🔨 Import both the elements.&lt;/li&gt;
&lt;li&gt;🧠 Telling Dnd what to do on starting and ending the drag.&lt;/li&gt;
&lt;li&gt;🔜 Playing with &lt;em&gt;DragOverlay&lt;/em&gt; to have smooth transitions (coming soon, stay tuned).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  1. 🛠️ Creating project with vite
&lt;/h2&gt;

&lt;p&gt;Use &lt;code&gt;npm create vite@latest . -- --template react&lt;/code&gt;&lt;br&gt;
if you are already in the folder where you wish to create or try or&lt;br&gt;
&lt;code&gt;npm create vite@latest my-proj -- --template react&lt;/code&gt; which will create a directory with name &lt;code&gt;my-proj&lt;/code&gt;.&lt;br&gt;
In the last command the &lt;code&gt;.&lt;/code&gt; denoted that we want to create this react project in the directory we are in.&lt;/p&gt;

&lt;p&gt;run &lt;code&gt;npm install&lt;/code&gt; to install the required node packages.&lt;/p&gt;

&lt;p&gt;Remove everything from the &lt;code&gt;App.jsx&lt;/code&gt; it should look something like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "./App.css";
function App() {
  return &amp;lt;&amp;gt;Hello&amp;lt;/&amp;gt;;
}

export default App;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will give us a clean slate to work with.&lt;br&gt;
I have kept the default styles from the template because I am lazy.&lt;/p&gt;
&lt;h2&gt;
  
  
  2. 🪄 Installing Dnd-kit and wrapping in DndContext
&lt;/h2&gt;

&lt;p&gt;We can use &lt;code&gt;npm install @dnd-kit/core&lt;/code&gt; to install Dnd-kit to our project.&lt;/p&gt;

&lt;p&gt;once installed we can wrap our webapp in DndContext which is like the central controller for Dnd-kit, and without this drag and drop will not work Also add some containers to keep our items in the future. Our &lt;code&gt;App.jsx&lt;/code&gt; will look something like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { DndContext } from "@dnd-kit/core";
import "./App.css";

function App() {
  return (
    &amp;lt;&amp;gt;
      &amp;lt;DndContext&amp;gt;
        &amp;lt;div className="webapp"&amp;gt;
          &amp;lt;div className="droppable-container"&amp;gt;&amp;lt;/div&amp;gt;

          &amp;lt;div className="draggable-container"&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/DndContext&amp;gt;
    &amp;lt;/&amp;gt;
  );
}

export default App;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. 👀 Basic Page Setup 👀
&lt;/h2&gt;

&lt;p&gt;I just created 2 containers one will hold all the draggable items initially and all elements will be dropped in to the droppable container which we will change soon. This is how the &lt;code&gt;App.css&lt;/code&gt; looks like after the changes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;body {
  height: 100vh;
  width: 100vw;
  margin: 0;
  padding: 0;
}
#root {
  height: 100%;
  width: 100%;
}

.webapp {
  height: 100%;
  width: 100%;
  display: flex;
  gap: 5%;
  justify-content: space-evenly;
  align-items: center;
}
.droppable-container {
  outline: green solid;
  width: 40%;
  height: 80%;
  display: grid;
  grid-template-columns: auto auto auto;
  align-items: center;
  justify-content: space-evenly;
}

.draggable-container {
  outline: solid red;
  width: 40%;
  height: 80%;
  display: grid;
  grid-template-columns: auto auto auto;
  align-items: center;
  justify-content: space-evenly;

}

.drag-item {
  background-color: antiquewhite;
  height: 100px;
  width: 100px;
  cursor: pointer;
  color: black;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. 🖱️ Let's Create a simple Draggable component
&lt;/h2&gt;

&lt;p&gt;Create a components directory in src and a 'draggableItem' directory within this directory.&lt;br&gt;
Then create a &lt;code&gt;DraggableItem.jsx&lt;/code&gt; file.&lt;br&gt;
As mentioned most of the heavy lifting gets done by Dnd-kit so following the documentation the Draggable Item will look something like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useDraggable } from "@dnd-kit/core";
import { CSS } from "@dnd-kit/utilities";
const Draggable = (props) =&amp;gt; {
  const { attributes, listeners, setNodeRef, transform } = useDraggable({
    id: props.id,
  });
  const style = {
    transform: CSS.Translate.toString(transform),
  };

  return (
    &amp;lt;div
      className="drag-item"
      style={style}
      ref={setNodeRef}
      {...listeners}
      {...attributes}
    &amp;gt;
      {props.children}
    &amp;lt;/div&amp;gt;
  );
};

export default Draggable;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;props.id&lt;/code&gt; is the most important as it defines what element becomes draggable if you plan on having just one then you can hard assign the id in the component but if you are creating multiple then it is better to receive it from the parent.&lt;br&gt;
and using   &lt;code&gt;const { attributes, listeners, setNodeRef, transform } = useDraggable({     id: props.id,   });&lt;/code&gt;  spreads all the needed stuff from Dnd-kit.&lt;/p&gt;

&lt;p&gt;and this line   &lt;code&gt;const style = {     transform: CSS.Translate.toString(transform),   };&lt;/code&gt; makes the element visually move on the screen.&lt;/p&gt;

&lt;p&gt;and here we basically assign everything to our draggable container&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return (
    &amp;lt;div
      className="drag-item"
      style={style}
      ref={setNodeRef}
      {...listeners}
      {...attributes}
    &amp;gt;
      {props.children}
    &amp;lt;/div&amp;gt;
  );

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;props.children is basically everything that we will wrap under the &lt;code&gt;Draggable&lt;/code&gt; component in the main file &lt;code&gt;App.jsx&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  5. 📦 Let's create a Droppable component
&lt;/h2&gt;

&lt;p&gt;Once done the &lt;code&gt;Droppable.jsx&lt;/code&gt; should look something like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useDroppable } from "@dnd-kit/core";
const Droppable = (props) =&amp;gt; {
  const { isOver, setNodeRef } = useDroppable({
    id: "drop-container",
  });
  return (
    &amp;lt;div className="droppable-container" ref={setNodeRef}&amp;gt;     
      {props.children}    
    &amp;lt;/div&amp;gt;
  );
};

export default Droppable;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we only want a single droppable container so we can assign the id directly here. and the &lt;code&gt;isOver&lt;/code&gt; tells DndContext if an element is over this droppable container, and once we drop the item on this container we use &lt;code&gt;props.children&lt;/code&gt; to render all the dropped items in the droppable container.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. 🔨 Let's import both the elements to &lt;code&gt;App.jsx&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;I have also created an array of some items so we can have multiple draggable items and then we can map over them to create the draggable items we need unique id's for all so we will use the index to create these Draggable items.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { DndContext } from "@dnd-kit/core";
import "./App.css";
import Droppable from "./components/droppableItem/Droppable";
import Draggable from "./components/draggableItem/Draggable";
import { useState } from "react";
function App() {
  const [items, setItems] = useState(["Cat", "Dog", "Mansion", "Car", "Lake"]);

  return (
  &amp;lt;&amp;gt;
      &amp;lt;DndContext onDragStart={onDragStart} onDragEnd={onDragEnd}&amp;gt;
        &amp;lt;div className="webapp"&amp;gt;
            &amp;lt;Droppable&amp;gt;
              {droppedItems.map((id) =&amp;gt; (
                &amp;lt;Draggable key={id} id={id}&amp;gt;
                  {`${id}`}
                &amp;lt;/Draggable&amp;gt;
              ))}
            &amp;lt;/Droppable&amp;gt;
          &amp;lt;div className="draggable-container"&amp;gt;
            {items.map((id) =&amp;gt; (
              &amp;lt;Draggable key={id} id={id}&amp;gt;
                {`${id}`}
              &amp;lt;/Draggable&amp;gt;
            ))}
          &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/DndContext&amp;gt;
    &amp;lt;/&amp;gt;
  );
}

export default App;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also make some styling changes to the Draggable container so that all elements are spread out properly&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.draggable-container {
  outline: solid red;
  width: 40%;
  height: 80%;
  display: grid;
  grid-template-columns: auto auto auto;
  align-items: center;
  justify-content: space-evenly;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and also style the drag Items.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.drag-item {
  background-color: antiquewhite;
  height: 100px;
  width: 100px;
  cursor: pointer;
  color: black;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point you should be able to at least drag around your items.&lt;br&gt;
But you won't be able to drop them anymore, that is because we still have not configured the Drop logic. let's get to it.&lt;/p&gt;
&lt;h2&gt;
  
  
  7. 🧠 Telling DND what to do on Drag start and End.
&lt;/h2&gt;

&lt;p&gt;To do so we will create 2 functions &lt;code&gt;onDragStart&lt;/code&gt; and &lt;code&gt;onDragEnd&lt;/code&gt; and create some state variables &lt;code&gt;const [droppedItems, setDroppedItems] = useState([])&lt;/code&gt; This will hold the list of all the elements dropped in the container. &lt;code&gt;const [activeId, setActiveId] = useState(null)&lt;/code&gt; and this will keep track of what element we are dragging. and set them in the dndContext.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const [droppedItems, setDroppedItems] = useState([])
  const [activeId, setActiveId] = useState(null)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;&amp;lt;DndContext onDragStart={onDragStart} onDragEnd={onDragEnd}&amp;gt;&lt;/code&gt;&lt;br&gt;
first on dragStart we need to set the &lt;code&gt;activeId&lt;/code&gt; to the element being dragged.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const onDragStart = (event) =&amp;gt;{
    setActiveId(event.active.id)
  }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And when the drag ends&lt;br&gt;
We need to check if it was dropped over our droppable container or not and if it was then check if that item already existed in there or not we do not want to duplicate the same element, then we need to remove the dragged element from the &lt;code&gt;items&lt;/code&gt; list to the &lt;code&gt;droppedItems&lt;/code&gt; list and remember to remove the &lt;code&gt;activeId&lt;/code&gt;.&lt;br&gt;
Something like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const onDragEnd = (event) =&amp;gt; {
    const {active, over} = event
    if (!over) return
    if(over.id ==='drop-container'){
        setItems(items =&amp;gt; items.filter(item =&amp;gt; item!= activeId))
        setDroppedItems([activeId, ...droppedItems])
    }
    setActiveId(null)
  }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After this you should be able to drop your elements to the droppable container, but you cannot remove them from it, but it is really easy to do so as well.&lt;/p&gt;

&lt;p&gt;This time:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We will check if the item was dropped over nothing (outside the container).&lt;/li&gt;
&lt;li&gt;Check if that element was inside the droppable container before the drag or not.&lt;/li&gt;
&lt;li&gt;if not, do nothing element was out stays out&lt;/li&gt;
&lt;li&gt;if yes, remove from the &lt;code&gt;droppedItems&lt;/code&gt; list and add back to the &lt;code&gt;items&lt;/code&gt; list.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And now your &lt;code&gt;dragEnd&lt;/code&gt; function should look something like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const onDragEnd = (event) =&amp;gt; {
    const {active, over} = event
    if (!over){
      if (droppedItems.includes(activeId)){
        setDroppedItems((items) =&amp;gt; items.filter((item) =&amp;gt; item != activeId));
        setItems([...items, activeId])
      }
      setActiveId(null)
      return
    }
    if(over.id ==='drop-container' &amp;amp;&amp;amp; !droppedItems.includes(activeId)){
        setItems(items =&amp;gt; items.filter(item =&amp;gt; item!= activeId))
        setDroppedItems([activeId, ...droppedItems])
    }
    setActiveId(null)
  }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and you should be able to drag and drop stuff in the container and outside the container to remove it.&lt;/p&gt;

&lt;h2&gt;
  
  
  8.🧠 Playing with &lt;em&gt;DragOverlay&lt;/em&gt; to have smooth transitions
&lt;/h2&gt;

&lt;p&gt;We can also animate the drag and drop of the elements with the help of &lt;code&gt;dragOverlay&lt;/code&gt; which is also very easy. I will be creating a new article for the same soon, so stay tuned for that :)&lt;/p&gt;

&lt;p&gt;Feel free to drop a comment in case something seems wrong or you are unable to replicate the same functionality, would love to help 😄.&lt;br&gt;
In the end here is all the code and the 🔗 &lt;a href="https://github.com/tushar-droid/Dnd-kit-Tutorial.git" rel="noopener noreferrer"&gt;git link&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  App.jsx
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { DndContext } from "@dnd-kit/core";
import "./App.css";
import Droppable from "./components/droppableItem/Droppable";
import Draggable from "./components/draggableItem/Draggable";
import { useState } from "react";
function App() {
  const [items, setItems] = useState(["Cat", "Dog", "Mansion", "Car", "Lake"]);
  const [droppedItems, setDroppedItems] = useState([])
  const [activeId, setActiveId] = useState(null)

  const onDragStart = (event) =&amp;gt;{
    setActiveId(event.active.id)
  }

  const onDragEnd = (event) =&amp;gt; {
    const {active, over} = event
    if (!over){
      if (droppedItems.includes(activeId)){
        setDroppedItems((items) =&amp;gt; items.filter((item) =&amp;gt; item != activeId));
        setItems([...items, activeId])
      }
      setActiveId(null)
      return
    }
    if(over.id ==='drop-container' &amp;amp;&amp;amp; !droppedItems.includes(activeId)){
        setItems(items =&amp;gt; items.filter(item =&amp;gt; item!= activeId))
        setDroppedItems([activeId, ...droppedItems])
    }
    setActiveId(null)
  }

  return (
    &amp;lt;&amp;gt;
      &amp;lt;DndContext onDragStart={onDragStart} onDragEnd={onDragEnd}&amp;gt;
        &amp;lt;div className="webapp"&amp;gt;
            &amp;lt;Droppable&amp;gt;
              {droppedItems.map((id) =&amp;gt; (
                &amp;lt;Draggable key={id} id={id}&amp;gt;
                  {`${id}`}
                &amp;lt;/Draggable&amp;gt;
              ))}
            &amp;lt;/Droppable&amp;gt;
          &amp;lt;div className="draggable-container"&amp;gt;
            {items.map((id) =&amp;gt; (
              &amp;lt;Draggable key={id} id={id}&amp;gt;
                {`${id}`}
              &amp;lt;/Draggable&amp;gt;
            ))}
          &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/DndContext&amp;gt;
    &amp;lt;/&amp;gt;
  );
}

export default App;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Droppable.jsx
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useDroppable } from "@dnd-kit/core";
const Droppable = (props) =&amp;gt; {
  const { isOver, setNodeRef } = useDroppable({
    id: "drop-container",
  });
  return (
    &amp;lt;div className="droppable-container" ref={setNodeRef}&amp;gt;

      {props.children}

    &amp;lt;/div&amp;gt;
  );
};

export default Droppable;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Draggable.jsx
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useDraggable } from "@dnd-kit/core";
import { CSS } from "@dnd-kit/utilities";
const Draggable = (props) =&amp;gt; {
  const { attributes, listeners, setNodeRef, transform } = useDraggable({
    id: props.id,
  });
  const style = {
    transform: CSS.Translate.toString(transform),
  };

  return (
    &amp;lt;div
      className="drag-item"
      style={style}
      ref={setNodeRef}
      {...listeners}
      {...attributes}
    &amp;gt;
      {props.children}
    &amp;lt;/div&amp;gt;
  );
};

export default Draggable;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  App.css
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;body {
  height: 100vh;
  width: 100vw;
  margin: 0;
  padding: 0;
}
#root {
  height: 100%;
  width: 100%;
}

.webapp {
  height: 100%;
  width: 100%;
  display: flex;
  gap: 5%;
  justify-content: space-evenly;
  align-items: center;
}
.droppable-container {
  outline: green solid;
  width: 40%;
  height: 80%;
  display: grid;
  grid-template-columns: auto auto auto;
  align-items: center;
  justify-content: space-evenly;
}

.draggable-container {
  outline: solid red;
  width: 40%;
  height: 80%;
  display: grid;
  grid-template-columns: auto auto auto;
  align-items: center;
  justify-content: space-evenly;

}

.drag-item {
  background-color: antiquewhite;
  height: 100px;
  width: 100px;
  cursor: pointer;
  color: black;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cover image taken from &lt;a href="https://www.craiyon.com/image/ATIag5MkSdSM35mbTPkJMw" rel="noopener noreferrer"&gt;craiyon AI &lt;/a&gt;&lt;/p&gt;

</description>
      <category>draganddrop</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>react</category>
    </item>
    <item>
      <title>Request Parameters getting set to favicon.ico on GET requests</title>
      <dc:creator>Tushar Tushar</dc:creator>
      <pubDate>Tue, 04 Mar 2025 23:37:16 +0000</pubDate>
      <link>https://dev.to/tushar_sandhu/request-parameters-getting-set-to-faviconico-on-get-requests-2b7</link>
      <guid>https://dev.to/tushar_sandhu/request-parameters-getting-set-to-faviconico-on-get-requests-2b7</guid>
      <description>&lt;p&gt;You’ve just deployed your first backend web app, ready to accept requests, and everything seems to be running smoothly. Then, out of nowhere, you get a notification that your web app has crashed. You check the error and see:&lt;br&gt;
&lt;code&gt;invalid input: "favicon.ico"&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5bos7fcw6vok7lsnb1au.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5bos7fcw6vok7lsnb1au.png" alt="Favicon Error" width="406" height="82"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The term sounds familiar, but you’re sure you’ve never used anything like this in your backend. What is it?&lt;/p&gt;

&lt;p&gt;Well, favicon stands for "favorite icon," and it’s the small icon that appears in your browser tab next to the page title. You’ve probably noticed it — it’s that little logo next to the website’s name in your browser. Yep, that’s the one!&lt;/p&gt;

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

&lt;p&gt;Let me guess — you never set this up for your app because it was just a quick project to help you get familiar with HTTP, Node, or Express, or something similar. You might not even want a favicon, because, like me, you're just too lazy to bother with it 😅.&lt;/p&gt;

&lt;p&gt;So, why did this happen?&lt;/p&gt;

&lt;p&gt;Browsers automatically try to request /favicon.ico from the root directory to display the icon in the browser tab. When that request fails, your app crashes.&lt;/p&gt;

&lt;p&gt;How can you fix it?&lt;/p&gt;

&lt;p&gt;There are two ways:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The easy way: Catch the favicon request and send a No Content status.
javascript
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.get('/favicon.ico', (req, res) =&amp;gt; {
  res.status(204).end();
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Or: Provide a favicon.ico file in the root directory of your app.
Both methods will prevent the app from crashing.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>node</category>
      <category>express</category>
    </item>
    <item>
      <title>Hacking Leetcode runtimes!!</title>
      <dc:creator>Tushar Tushar</dc:creator>
      <pubDate>Mon, 24 Feb 2025 20:19:25 +0000</pubDate>
      <link>https://dev.to/tushar_sandhu/hacking-leetcode-runtimes-5blo</link>
      <guid>https://dev.to/tushar_sandhu/hacking-leetcode-runtimes-5blo</guid>
      <description>&lt;p&gt;In my opinion it is way bigger achievement than actually solving that particular problem in the lowest time whoever did this, you my friend.. you have earned my respect!!&lt;/p&gt;

&lt;p&gt;While working through a recent LeetCode problem after solving it I went on to check out the best solutions and other ways to solve the problem and I came across one really bright mind.&lt;br&gt;
One of the solutions had a piece of code that reverse-engineered how LeetCode displays runtime results. Yep, someone actually figured out a way to modify the runtime display to always show 0 ms, regardless of the actual performance of the algorithm. &lt;br&gt;
This code essentially manipulates the system and writes "0" into a file when the program exits, effectively "hiding" the real performance.&lt;br&gt;
it uses a module called &lt;code&gt;atexit&lt;/code&gt; i.e. "at exit" which then receives a function to register and unregistered cleanup functions, and then it takes a lambda function which opens the "display_runtime.txt" and writes 0 to it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fph2jkc6wieqib98sm58l.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fph2jkc6wieqib98sm58l.jpg" alt="Screenshot from leetcode solution" width="800" height="208"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl3t2wfcsdju8px5kk6o6.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl3t2wfcsdju8px5kk6o6.jpg" alt="Screenshot about the atexit package" width="800" height="227"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>What is Express</title>
      <dc:creator>Tushar Tushar</dc:creator>
      <pubDate>Sat, 18 Jan 2025 19:04:23 +0000</pubDate>
      <link>https://dev.to/tushar_sandhu/what-is-express-149d</link>
      <guid>https://dev.to/tushar_sandhu/what-is-express-149d</guid>
      <description>&lt;p&gt;Express is a backend Framework which makes our job easier by providing us with most of the things that we need to quite often so we  can use them rather than creating them again and again.&lt;/p&gt;

&lt;p&gt;for example&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Authentication&lt;/li&gt;
&lt;li&gt;Setting up a server&lt;/li&gt;
&lt;li&gt;Requests to other API’s&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setting up a Basic server with Express
&lt;/h2&gt;

&lt;p&gt;Here is the code to set up a basic server with the help of Express&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="n"&gt;Import&lt;/span&gt; &lt;span class="n"&gt;Express&lt;/span&gt;
&lt;span class="n"&gt;const&lt;/span&gt; &lt;span class="n"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'express'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="k"&gt;Create&lt;/span&gt; &lt;span class="n"&gt;an&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;express&lt;/span&gt;
&lt;span class="n"&gt;const&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;express&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="k"&gt;Create&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;route&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;incoming&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;
&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="n"&gt;callback&lt;/span&gt; &lt;span class="k"&gt;to&lt;/span&gt; &lt;span class="n"&gt;respond&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;"Hello world"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="n"&gt;Usually&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;defined&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;Environment&lt;/span&gt; &lt;span class="n"&gt;variables&lt;/span&gt;
&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="n"&gt;const&lt;/span&gt; &lt;span class="n"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;
&lt;span class="n"&gt;const&lt;/span&gt; &lt;span class="n"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;

&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="n"&gt;listening&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="k"&gt;defined&lt;/span&gt; &lt;span class="n"&gt;PORT&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Now listening on PORT 3000'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we make any changes we will have to restart the server again and again to avoid that we use the &lt;strong&gt;watch&lt;/strong&gt; flag offered by node&lt;/p&gt;

&lt;p&gt;So to run the app use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="c1"&gt;--watch app.js&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is an example of a very basic Routing APP&lt;/p&gt;

&lt;p&gt;Here is an example of a very basic routing APP, which takes the HTML views from the folder views.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;const&lt;/span&gt; &lt;span class="n"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;"express"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;const&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;"path"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;const&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sendFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;"views/index.html"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;"/aboutme"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sendFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;"views/about.html"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;"/contact-me"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sendFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;"views/contact-me.html"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;"*"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sendFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;"views/404.html"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;"The App is listening on PORT 3000 now. "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>🌟 Automating API Calls with Azure and Power Automate</title>
      <dc:creator>Tushar Tushar</dc:creator>
      <pubDate>Sun, 05 Jan 2025 05:15:54 +0000</pubDate>
      <link>https://dev.to/tushar_sandhu/automating-api-calls-with-azure-and-power-automate-5haf</link>
      <guid>https://dev.to/tushar_sandhu/automating-api-calls-with-azure-and-power-automate-5haf</guid>
      <description>&lt;p&gt;I recently worked on an interesting project that required integrating with an API for data retrieval. The challenge? While Power Automate was perfect for automation, doesn’t support HMAC signing out of the box. But using the provider's library could help us avoid HMAC, but Power Automate cannot run scripts on its own.&lt;/p&gt;

&lt;p&gt;Here’s how I solved it:&lt;/p&gt;

&lt;p&gt;🔹 Created an Azure Function App to handle the API integration.&lt;br&gt;
🔹 The Function App runs a Python function that processes HTTPS GET requests with API keys in the headers and then uses the provider’s library to fetch the required data.&lt;br&gt;
🔹 To ensure security, I had to figure out how to use environment variables in the cloud function to securely store sensitive information like API keys.&lt;br&gt;
🔹 Then, I integrated this with Power Automate to automate weekly queries to the Function App and send the fetched data via email.&lt;/p&gt;

&lt;p&gt;This project was not only a solution to the problem but also an opportunity to:&lt;br&gt;
💡 Learn and work with Azure Function Apps, diving deeper into cloud computing.&lt;br&gt;
💡 Explore Power Automate for creating seamless workflows.&lt;br&gt;
💡 Gain hands-on experience with securing cloud functions using environment variables to protect sensitive data.&lt;/p&gt;

&lt;p&gt;I’m thrilled with how these tools came together to make the process efficient and scalable.&lt;/p&gt;

&lt;p&gt;Have you faced a similar challenge or used Azure and Power Automate in your projects? I’d love to hear your thoughts and experiences!&lt;/p&gt;

&lt;p&gt;Let me know if you would like me to maybe create an example app on how to do the same. :) &lt;/p&gt;

&lt;h1&gt;
  
  
  CloudComputing #PowerAutomate #Azure #Automation
&lt;/h1&gt;

</description>
    </item>
    <item>
      <title>Creating a Deepcopy of Linkedlist with random pointers</title>
      <dc:creator>Tushar Tushar</dc:creator>
      <pubDate>Sun, 05 Jan 2025 01:37:47 +0000</pubDate>
      <link>https://dev.to/tushar_sandhu/creating-a-deepcopy-of-linkedlist-with-random-pointers-47pj</link>
      <guid>https://dev.to/tushar_sandhu/creating-a-deepcopy-of-linkedlist-with-random-pointers-47pj</guid>
      <description>&lt;p&gt;While solving a Leetcode problem yesterday I came across this pretty cool problem which is fairly very different then most that I have solved. Here we have to create a deep copy of a linked list which also has random pointers which can point to any other random node in the linked list or &lt;code&gt;None&lt;/code&gt;.&lt;br&gt;
Feel free to have a try yourself here &lt;a href="https://leetcode.com/problems/copy-list-with-random-pointer/description/" rel="noopener noreferrer"&gt;138. Copy List with Random Pointer&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A little weird problem here, firstly to clear some confusions, deep copy means a separate copy in the memory, basically if anything was to happen to the original list nothing would happed to the deep copy as that is a completely independent copy of the list.&lt;/p&gt;

&lt;p&gt;So to make a deep copy we will have to make brand new nodes and use the relationships in the original linked list to be properly mapped to the new ones.&lt;/p&gt;

&lt;p&gt;Coming to the random pointers, that means that every node in the linked list has a new variable apart from the usual value and next which is &lt;code&gt;random&lt;/code&gt; which can be pointing to any random node in the linked list or None.&lt;/p&gt;

&lt;p&gt;The best way to approach this problem is to first create a copy of all the nodes in the original lineked list and then use the original linked list to create all the connections or basically assign all the pointers.&lt;/p&gt;

&lt;p&gt;Now the first thing that might come to mind is just creating copies of the node using the Vals, of the original list, but how do you keep track of them, as without the pointers to the next node they might just get lost in the memories, we need to hold them somewhere.&lt;/p&gt;

&lt;p&gt;Also, to assign the pointer values to the new linked list we will need to access the values from the original linkedlist, and as we know finding a node in linked list takes &lt;code&gt;O(n)&lt;/code&gt; which is not the best. So, what the best solution in this case might be is to use a hash map, as that can help us store our new nodes without pointers as well as we can map the old nodes to the new ones while having a &lt;code&gt;constant O(1)&lt;/code&gt; time to look up values or nodes in this case.&lt;/p&gt;

&lt;p&gt;here goes nothing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#there already exists a Node class that only takes `x` as the input to create a new node.
#but also has 'random' and 'next' pointers to set.
&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;deepCopy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;hmap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="n"&gt;dummy_pointer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;dummy_pointer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;new_node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dummy_pointer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# In the origianl Llist, the x is represented as val
&lt;/span&gt;        &lt;span class="n"&gt;hmap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dummy_pointer&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new_node&lt;/span&gt;
        &lt;span class="n"&gt;dummy_pointer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dummy_pointer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;

    &lt;span class="n"&gt;dummy_pointer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;


    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;dummy_pointer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;curr_new_node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hmap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dummy_pointer&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

        &lt;span class="n"&gt;curr_new_node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hmap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dummy_pointer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;dummy_pointer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
        &lt;span class="n"&gt;curr_new_node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hmap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dummy_pointer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;dummy_pointer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
        &lt;span class="n"&gt;dummy_pointer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dummy_pointer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;


    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;hmap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now this might look like all, but there is an edge case that we also need to take care of, which is what if the original linkedlist is empty. Pretty straightforward just have a conditional in the beginning to check if the incoming Llist is empty if it is just return None.&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#there already exists a Node class that only takes `x` as the input to create a new node.
#but also has 'random' and 'next' pointers to set.
&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;deepCopy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
    &lt;span class="n"&gt;hmap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="n"&gt;dummy_pointer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;dummy_pointer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;new_node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dummy_pointer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# In the origianl Llist, the x is represented as val
&lt;/span&gt;        &lt;span class="n"&gt;hmap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dummy_pointer&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new_node&lt;/span&gt;
        &lt;span class="n"&gt;dummy_pointer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dummy_pointer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;

    &lt;span class="n"&gt;dummy_pointer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;


    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;dummy_pointer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;curr_new_node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hmap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dummy_pointer&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

        &lt;span class="n"&gt;curr_new_node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hmap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dummy_pointer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;dummy_pointer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
        &lt;span class="n"&gt;curr_new_node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hmap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dummy_pointer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;dummy_pointer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
        &lt;span class="n"&gt;dummy_pointer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dummy_pointer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;


    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;hmap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

</description>
    </item>
    <item>
      <title>Floyd’s Cyclic Algorithm</title>
      <dc:creator>Tushar Tushar</dc:creator>
      <pubDate>Thu, 02 Jan 2025 16:20:56 +0000</pubDate>
      <link>https://dev.to/tushar_sandhu/floyds-cyclic-algorithm-4ga9</link>
      <guid>https://dev.to/tushar_sandhu/floyds-cyclic-algorithm-4ga9</guid>
      <description>&lt;p&gt;This algorithm is used to find cycles in lists.&lt;/p&gt;

&lt;p&gt;The Idea is if we leave two pointers on the same list. if there are no loops in the list the fast loop will run into the end of the list or run into None.&lt;/p&gt;

&lt;p&gt;But if there are any loops present in the loop the two pointers are bound to run into each other.&lt;/p&gt;

&lt;p&gt;this will give us a truth or False value if there exists any loops in the list.&lt;/p&gt;

&lt;p&gt;To take it a step further to see where the loop starts there is some math involved.&lt;/p&gt;

&lt;p&gt;Given that we find the point where both the pointers meet, we can start 2 more pointer one will start from the point where the previous 2 pointers met and the other one starts at the beginning of the list and as per some maths attached in the picture next these 2 pointers are bound to meet at the beginning of the loop.&lt;/p&gt;

&lt;p&gt;One question you can solve using this algorithm is problem &lt;/p&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/problems/linked-list-cycle/description/" rel="noopener noreferrer"&gt;141. Linked List Cycle&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def hasCycle(self, head: Optional[ListNode]) -&amp;gt; bool:

        slow = head
        fast = head

        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
            if slow == fast:
                return True

        return False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fghkpvzykdvmc4hf3u7ju.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fghkpvzykdvmc4hf3u7ju.png" alt="Maths for Floyd's algorithm" width="800" height="511"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Feel Free to drop in any mistakes or any additions :) &lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
