Make Your React Components Sortable

Make Your React Components Sortable

Easy and effective sorting

I was recently working on a code challenge that involved adding/removing items from a list as well as organizing them in a particular order. To avoid any extraneous navigation or elements, I decided to implement drag-and-drop logic in order to make my items easy to move from one position to another.

There are a lot of great libraries for drag-and-drop functionality in React, but some of them require a bit more configuration and offer more than I needed. Eventually, I ended up using react-sortable-hoc.

react-sortable-hoc

react-sortable-hoc is an easy-to-use library when you need a list of components to be sortable. I was able to incorporate it with my own components in just a few minutes. For this demo, we’ll be creating a to-do React app (original, I know). Let’s kick things off by installing the package with npm i react-sortable-hoc.

For this article, I will also be installing array-move, which is a convenient package that helps you move items to different positions in an array. react-sortable-hoc is not dependent on array-move. I am merely using it for convenience.

Below is an array of to-do items:

const todosInit = [
  {id: 1, content: 'content 1', isDone: false},
  {id: 2, content: 'content 2', isDone: false},
  {id: 3, content: 'content 3', isDone: false},
  {id: 4, content: 'content 4', isDone: false},
  {id: 5, content: 'content 5', isDone: false}
];

For the app, we have two components. ToDoItem will be the to-do item's component. ToDoList is the component that renders a list of ToDoItems. Now for the two components provided by react-sortable-hoc.

reactjs programming react javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How native is React Native? | React Native vs Native App Development

Article covers: How native is react native?, React Native vs (Ionic, Cordova), Similarities and difference between React Native and Native App Development.

Increase Performance of React Applications Via Array JavaScript Methods

Increase Performance of React Applications Via Array JavaScript Methods. We will create a simple event management application in the react to add, update, and delete an event.

ReactJS: The Javascript Developer’s Guide

This article will walk you through the concepts you would need to know to step into the world of widely used ReactJS.

Forms of Composition in JavaScript and React

One of the core ideas in functional programming is composition: building larger things from smaller things. The canonical example of this idea should be familiar with legos.

The Ugly Side of React Hooks

In this post, I will share my own point of view about React Hooks, and as the title of this post implies, I am not a big fan.