<?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: Colin McDermott</title>
    <description>The latest articles on DEV Community by Colin McDermott (@colinmcd01).</description>
    <link>https://dev.to/colinmcd01</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%2F478878%2F13485fc8-e820-4cf6-be24-9983e48f84f6.jpeg</url>
      <title>DEV Community: Colin McDermott</title>
      <link>https://dev.to/colinmcd01</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/colinmcd01"/>
    <language>en</language>
    <item>
      <title>Git Submodules</title>
      <dc:creator>Colin McDermott</dc:creator>
      <pubDate>Tue, 30 Mar 2021 14:48:42 +0000</pubDate>
      <link>https://dev.to/colinmcd01/git-submodules-gbp</link>
      <guid>https://dev.to/colinmcd01/git-submodules-gbp</guid>
      <description>&lt;p&gt;This week I have been learning about Git Submodules. &lt;br&gt;
Submodules is a tool built into git. It's designed to make working with repos within repos a lot easier. Why would you be working with repos within repos? Well, let's talk about that. &lt;/p&gt;
&lt;h1&gt;
  
  
  Repo-ception
&lt;/h1&gt;

&lt;p&gt;Why would you use a repo inside another repo?&lt;br&gt;
There are several reasons you might want to do this. For example, if you have your own component library you use across multiple projects, and you want to work on both the component library and the app itself side by side. Or on a larger project containing multiple pieces worked on by multiple teams, when you don't want code from the various sections of the project interfering with one another during the development process.&lt;/p&gt;
&lt;h1&gt;
  
  
  Setting up your repo
&lt;/h1&gt;

&lt;p&gt;So to give a simplified example of this set up we are going to have a parent repo, and 2 child repos, childA and childB.&lt;br&gt;
First off, we need to clone the parent repo. To add the two child repos as submodules of parent we are going to go into the parent and run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add submodule &amp;lt;repo-address&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;for each of the children, replacing &lt;code&gt;&amp;lt;repo-address&amp;gt;&lt;/code&gt; with the address for the repo.&lt;/p&gt;

&lt;p&gt;Once you've done this, you'll notice a new file has been created: &lt;code&gt;.gitmodules&lt;/code&gt; Inside &lt;code&gt;.gitmodules&lt;/code&gt; you will notice entries for both childA and childB. 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;[submodule "childA"]
    path = childA
    url = https://github.com/path/to/childA
[submodule "childB"]
    path = childB
    url = https://github.com/path/to/childB
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running &lt;code&gt;git status&lt;/code&gt;, you should see an entry for childA and childB being created. We can now commit and push these changes. &lt;/p&gt;

&lt;p&gt;Congratulations! You have now set up childA and childB as submodules of your parent repo.&lt;/p&gt;

&lt;h1&gt;
  
  
  Using your submodules
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Cloning
&lt;/h2&gt;

&lt;p&gt;Now that you have the submodules set up, next time you clone parent you will see 2 empty directories, 1 for each of the children. Here is where you will start to see the benefits of using submodules.&lt;/p&gt;

&lt;p&gt;Before using submodules, to clone any child repos, you needed to go through and run a git clone for each repo. In this example that would mean only 2 commands, but in a large project where there could be 5 or 6 child repos, it could mean quite a few commands. But with submodules, all we need to do is run one single command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git submodule update --init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will then go and clone all the submodule repos into their respective directories. &lt;/p&gt;

&lt;h2&gt;
  
  
  Installing
&lt;/h2&gt;

&lt;p&gt;Now we have all our code checked out, we need to install it. Working with multiple repos this can be a pain, and very time-consuming, moving in and out of each of the repos to run &lt;code&gt;npm install&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Submodules has a solution! As part of the submodules suite of functionality, git contains a &lt;code&gt;foreach&lt;/code&gt; command. To use it, we need to pass the command we want to run to it, in this case &lt;code&gt;npm install&lt;/code&gt;, and it will then run that command in each of the submodules.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git submodule foreach 'npm install'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In summary, we're going to replace all of those commands, moving in and out of repos, and using &lt;code&gt;npm install&lt;/code&gt; in each one, with a single command executed in the parent.&lt;/p&gt;

&lt;h2&gt;
  
  
  foreach
&lt;/h2&gt;

&lt;p&gt;Let's take a second now and talk about &lt;code&gt;foreach&lt;/code&gt;. This is an incredibly useful little command as you can see. &lt;/p&gt;

&lt;p&gt;Want to run &lt;code&gt;npm install&lt;/code&gt; in each child?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git submodule foreach 'npm install'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Want to switch each child to the &lt;code&gt;dev&lt;/code&gt; branch and pull the latest code?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git submodule foreach 'git checkout dev &amp;amp;&amp;amp; git pull'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Want to stash all changes?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git submodule foreach 'git add . &amp;amp;&amp;amp; git stash'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I'm sure you get the idea. Anything you want to do in every child repo can be done with 1 command.&lt;/p&gt;

&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;Going back to our example, we now have a fully working version of our project, including all sub-repos, and all we needed to do is the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone https://github.com/path/to/parent
cd parent
git submodule update --init
npm install
git submodule foreach 'npm install'
npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This was just a simple example showcasing what git submodules can do. There are plenty more useful features that I would encourage you to check out. Git themselves have some great documentation on submodules available &lt;a href="https://git-scm.com/book/en/v2/Git-Tools-Submodules"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>git</category>
      <category>submodules</category>
      <category>javascript</category>
      <category>repos</category>
    </item>
    <item>
      <title>Faking Disabled Dropdowns</title>
      <dc:creator>Colin McDermott</dc:creator>
      <pubDate>Wed, 27 Jan 2021 15:57:29 +0000</pubDate>
      <link>https://dev.to/colinmcd01/faking-disabled-dropdowns-5ank</link>
      <guid>https://dev.to/colinmcd01/faking-disabled-dropdowns-5ank</guid>
      <description>&lt;h1&gt;
  
  
  Creative problem solving is key in a corporate environment
&lt;/h1&gt;

&lt;p&gt;Working in the corporate world things are not as easy as when you're working on your own little project in your spare time. This is not news to anyone. Sooner or later we all end up in a situation where the simple solution to a problem is not workable due to the large and complex nature of these companies. Whether its being unable to update a package, working with legacy code, or having to use internal tools or component libraries; There are always challenges working for a big company that you won't find anywhere else. That is when you have to get creative with your problem solving.&lt;/p&gt;

&lt;p&gt;Recently I ran into one of these issues myself, working for a big company, having to use an internally built component library that was sorely lacking in features. In this case, I was using a dropdown component built using &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;ul&amp;gt;&lt;/code&gt; tags, without an input element. This meant that, obviously, the component will be missing out on getting certain features for free that come as part of the input element. This included the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefdisabled" rel="noopener noreferrer"&gt;disabled&lt;/a&gt; feature.&lt;/p&gt;

&lt;p&gt;For those unaware, a html input can take a &lt;code&gt;disabled&lt;/code&gt; prop:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A Boolean attribute which, if present, indicates that the user should not be able to interact with the input.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We were using these dropdowns to allow users to filter data on the page. My task was to include a switch on the page, which would disable the dropdowns, preventing the user from changing the filters until the switch was turned off again. A simple problem in a different place. Since the component we were using did not have use of the input element's built in &lt;code&gt;disabled&lt;/code&gt; function, and the developer who built it, had not replicated it, there was no way to disable these dropdowns. &lt;/p&gt;

&lt;p&gt;I did not have access to the component library to make a change and try to add this feature in, nor did I have time to try to find who is in charge of this library and ask them to change it. I needed to get creative. So I faked it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Faking it
&lt;/h2&gt;

&lt;p&gt;Let's start off with the filters ready and working, and the new switch added next to them. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fx5niijpie7ck3iy35i5u.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fx5niijpie7ck3iy35i5u.JPG" alt="dropdowns enabled with switch off"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At this point the dropdowns work, and the switch does nothing. Since we can't just pass a &lt;code&gt;disabled&lt;/code&gt; prop to the component and watch it work, we need a new solution. I decided that if I can't stop the dropdowns from working, I could stop the user from clicking them. So I decided to add a blocker between the user and component. &lt;/p&gt;

&lt;p&gt;I added a &lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt; that will be added to the dropdown container whenever the switch is turned on. The &lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt; fills the container and sits on top of the component, so the user is clicking the &lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt; and not the actual component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.dropdown-disabled {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I then added a colour to the span, and made it transparent. This combined with changing the cursor to &lt;code&gt;cursor: not-allowed&lt;/code&gt; makes it look the dropdowns are greyed out, and unusable. And voilá, the user cannot use the dropdowns, so for all intents and purposes, they are now disabled, using little more than some creative problem solving and some css.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  box-shadow: inset 100px 100px rgba(0, 0, 0, 0.25);
  cursor: not-allowed;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8kziol1ms7rlvw7mlv65.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8kziol1ms7rlvw7mlv65.JPG" alt="dropdowns look disabled with switch on"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/r6s6k"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;How would you solve this issue? Have you any similar stories? Let me know in the comments, I'd love to hear them!&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>programming</category>
      <category>css</category>
    </item>
    <item>
      <title>Drag &amp; Drop re-ordering using HTML and React</title>
      <dc:creator>Colin McDermott</dc:creator>
      <pubDate>Tue, 13 Oct 2020 17:05:09 +0000</pubDate>
      <link>https://dev.to/colinmcd01/drag-drop-re-ordering-using-html-and-react-974</link>
      <guid>https://dev.to/colinmcd01/drag-drop-re-ordering-using-html-and-react-974</guid>
      <description>&lt;p&gt;Working with javascript these days, you will often run into a scenario where you want to render a list of items. But what happens when you want your user to be able to re-order those items on the fly? Well here I'm going to show you how to use HTML5's Drag and Drop (DnD) API, with React to easily let your users move things around 'til their heart's content.&lt;/p&gt;

&lt;h3&gt;
  
  
  First, we're going to need a list of things to render!
&lt;/h3&gt;

&lt;p&gt;We'll start with a simple React App that renders 3 colourful boxes on the screen.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnt6by8yoe07bbjy58sba.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnt6by8yoe07bbjy58sba.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;App.js&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import React, { useState } from "react";
import Box from "./Box";
import "./styles.css";

const App = () =&amp;gt; {
  const [boxes, setBoxes] = useState([
    {
      id: "Box-1",
      color: "red",
      order: 1
    },
    {
      id: "Box-2",
      color: "green",
      order: 2
    },
    {
      id: "Box-3",
      color: "blue",
      order: 3
    }
  ]);

  return (
    &amp;lt;div className="App"&amp;gt;
      {boxes
        .sort((a, b) =&amp;gt; a.order - b.order)
        .map((box) =&amp;gt; (
          &amp;lt;Box
            key={box.id}
            boxColor={box.color}
            boxNumber={box.id}
          /&amp;gt;
        ))}
    &amp;lt;/div&amp;gt;
  );
}

export default App;


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

&lt;/div&gt;

&lt;p&gt;Box.js&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import React from "react";

const Box = ({ boxColor, boxNumber }) =&amp;gt; {
  return (
    &amp;lt;div
      id={boxNumber}
      style={{
        backgroundColor: boxColor,
        border: "1px solid",
        borderColor: boxColor,
        borderRadius: "5px",
        color: "#FFF",
        width: "30%",
        height: "100px"
      }}
    &amp;gt;
      {boxNumber}
    &amp;lt;/div&amp;gt;
  );
};

export default Box;



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

&lt;/div&gt;

&lt;p&gt;This should render your boxes like the ones in the picture above. But they don't do anything yet!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note the way the boxes are sorted to be rendered based on the &lt;code&gt;order&lt;/code&gt; attribute in their objects&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Next step is going to be introducing the DnD API into our boxes.
&lt;/h3&gt;

&lt;p&gt;To do this, we're going to go back into Box.js and add some attributes to the &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;. We're going to change it to: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const Box = ({ boxColor, boxNumber, handleDrag, handleDrop }) =&amp;gt; {
  return (
    &amp;lt;div
      draggable={true}
      id={boxNumber}
      onDragOver={(ev) =&amp;gt; ev.preventDefault()}
      onDragStart={handleDrag}
      onDrop={handleDrop}
      style={{
        backgroundColor: boxColor,
        border: "1px solid",
        borderColor: boxColor,
        borderRadius: "5px",
        color: "#FFF",
        width: "30%",
        height: "100px"
      }}
    &amp;gt;
      {boxNumber}
    &amp;lt;/div&amp;gt;
  );
};



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

&lt;/div&gt;

&lt;p&gt;First thing to note is that we are now taking in two extra props, &lt;code&gt;handleDrag&lt;/code&gt; and &lt;code&gt;handleDrop&lt;/code&gt;. These are just functions we are going to pass down from App.js to handle what happens when you drag and drop the box respectively.&lt;/p&gt;

&lt;p&gt;We've also added some attributes to the &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;.&lt;br&gt;
I'm not going to do into too much detail about what each of these attributes does, but briefly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;draggable&lt;/code&gt; sets whether the element can be dragged or not;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;onDragStart&lt;/code&gt; is an event listener triggered when you start dragging the element;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;onDrop&lt;/code&gt; is an event listener triggered when you drop the element;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;onDragOver&lt;/code&gt; is an event listener triggered when you drag the element over something else;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We're going to set &lt;code&gt;onDragStart&lt;/code&gt; to the &lt;code&gt;handleDrag&lt;/code&gt; prop we've just passed in, and &lt;code&gt;onDrop&lt;/code&gt; to the &lt;code&gt;handleDrop&lt;/code&gt; prop.&lt;/p&gt;

&lt;p&gt;For &lt;code&gt;onDragOver&lt;/code&gt; we're going to set a function to prevent the browser's default action, which is usually to attempt to navigate to a link or something like that.&lt;/p&gt;

&lt;h3&gt;
  
  
  Now for App.js.
&lt;/h3&gt;

&lt;p&gt;Here we're going to add in the &lt;code&gt;handleDrag&lt;/code&gt; and &lt;code&gt;handleDrop&lt;/code&gt; functions, and then we're going to pass them down into the Box component. &lt;/p&gt;

&lt;p&gt;So we'll take these one at a time, starting with &lt;code&gt;handleDrag&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

  const [dragId, setDragId] = useState();

  const handleDrag = (ev) =&amp;gt; {
    setDragId(ev.currentTarget.id);
  };


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

&lt;/div&gt;

&lt;p&gt;We've added a new state variable called &lt;code&gt;dragId&lt;/code&gt; to keep track of which box it is we're currently dragging. Inside the &lt;code&gt;handleDrag&lt;/code&gt; function itself all we're doing is getting the box id from the event and setting it to state.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;handleDrop&lt;/code&gt; is the more complicated of the two functions, and this is where we will handle all of our 'switching' code.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

  const handleDrop = (ev) =&amp;gt; {
    const dragBox = boxes.find((box) =&amp;gt; box.id === dragId);
    const dropBox = boxes.find((box) =&amp;gt; box.id === ev.currentTarget.id);

    const dragBoxOrder = dragBox.order;
    const dropBoxOrder = dropBox.order;

    const newBoxState = boxes.map((box) =&amp;gt; {
      if (box.id === dragId) {
        box.order = dropBoxOrder;
      }
      if (box.id === ev.currentTarget.id) {
        box.order = dragBoxOrder;
      }
      return box;
    });

    setBoxes(newBoxState);
  };


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

&lt;/div&gt;

&lt;p&gt;Here, we first want to identify which box is being dragged and which box it has been dropped on. We do this using the array &lt;code&gt;find()&lt;/code&gt; method and comparing each box id with the &lt;code&gt;dragId&lt;/code&gt; (that we set in &lt;code&gt;handleDrag&lt;/code&gt;) for the box being dragged, and with the id of the element emitting the event for the box being dropped on.&lt;/p&gt;

&lt;p&gt;Because we're going to change the order of the boxes, we don't want the original order of our two boxes to be changed, so we're going to take note of that in the &lt;code&gt;dragBoxOrder&lt;/code&gt; and &lt;code&gt;dropBoxOrder&lt;/code&gt; variables.&lt;/p&gt;

&lt;p&gt;Finally, we're going to the actual switch.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

    const newBoxState = boxes.map((box) =&amp;gt; {
      if (box.id === dragId) {
        box.order = dropBoxOrder;
      }
      if (box.id === ev.currentTarget.id) {
        box.order = dragBoxOrder;
      }
      return box;
    });


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

&lt;/div&gt;

&lt;p&gt;We're going to use the array &lt;code&gt;map()&lt;/code&gt; function to allow us to rearrange the boxes order and return as a new array. Inside the &lt;code&gt;map()&lt;/code&gt; function we'll check if the current box's id is equal to the dragId. If it is, set its order to be the dropBoxOrder. If not, check if it is equal to the id of the box being dropped on, and if that is true set its order to be the dragBoxOrder.&lt;/p&gt;

&lt;p&gt;So when the &lt;code&gt;map()&lt;/code&gt; function has stopped running we should have a new array in the &lt;code&gt;newBoxState&lt;/code&gt; variable where the order variable for the two boxes involved has been swapped. We can then set this new array of box objects to state, and trigger the re-render.&lt;/p&gt;

&lt;p&gt;To see the complete code, or play with the completed demo, check out this codesandbox:&lt;br&gt;
&lt;a href="https://codesandbox.io/s/react-drag-drop-reorder-mxt4t?fontsize=14&amp;amp;hidenavigation=1&amp;amp;theme=dark" rel="noopener noreferrer"&gt;https://codesandbox.io/s/react-drag-drop-reorder-mxt4t?fontsize=14&amp;amp;hidenavigation=1&amp;amp;theme=dark&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>html</category>
    </item>
  </channel>
</rss>
