search
HomeWeb Front-endCSS TutorialDraggin' and Droppin' in React

Draggin' and Droppin' in React

React offers numerous drag-and-drop libraries, but many require significant effort for even simple demos or lack advanced features like multiple drag-and-drop instances. react-sortable-hoc provides a streamlined solution using higher-order components (HOCs) to add drag-and-drop functionality. This tutorial assumes basic React and React Hooks knowledge.

We'll build an app with draggable GIFs (courtesy of Chris Gannon).

Project Setup:

Use create-react-app to create a new project:

npx create-react-app your-project-name

Install necessary packages:

cd your-project-name
yarn add react-sortable-hoc array-move

Styling, Data, and GIF Component:

Style your app (in App.css):

.App { background: #1a1919; color: #fff; min-height: 100vh; padding: 25px; text-align: center; }
.App h1 { font-size: 52px; margin: 0; }
.App h2 { color: #f6c945; text-transform: uppercase; }
.App img { cursor: grab; height: 180px; width: 240px; }

Define GIF data using useState:

import React, { useState } from 'react';
// ...
const [Draggin' and Droppin' in Reacts, setGifs] = useState([ /* GIF URLs */ ]);

Create a simple GIF component (Gif.js):

import React from 'react';
import PropTypes from 'prop-types';

const Gif = ({ Draggin' and Droppin' in React }) => <img alt="Draggin' and Droppin' in React" src="%7BDraggin'" and droppin in react>;

Gif.propTypes = { Draggin' and Droppin' in React: PropTypes.string.isRequired };

export default Gif;

Implementing Drag and Drop:

Import necessary HOCs and arrayMove:

import { sortableContainer, sortableElement } from 'react-sortable-hoc';
import arrayMove from 'array-move';

Create HOC wrappers:

const SortableGifsContainer = sortableContainer(({ children }) => <div classname="Draggin' and Droppin' in Reacts">{children}</div>);
const SortableGif = sortableElement(({ Draggin' and Droppin' in React }) => <gif draggin and droppin in react="{Draggin'"></gif>);

Wrap GIFs with HOCs and handle sorting:

const onSortEnd = ({ oldIndex, newIndex }) => setGifs(arrayMove(Draggin' and Droppin' in Reacts, oldIndex, newIndex));

// ... in your render method ...
<sortablegifscontainer axis="x" onsortend="{onSortEnd}">
  {Draggin' and Droppin' in Reacts.map((Draggin' and Droppin' in React, i) => <sortablegif draggin and droppin in react="{Draggin'" index="{i}" key="{Draggin'"></sortablegif>)}
</sortablegifscontainer>

Multiple Lists:

Add a second GIF array:

const [newGifs, setNewGifs] = useState([ /* GIF URLs */ ]);

Add collection prop to SortableGif and update onSortEnd:

const onSortEnd = ({ oldIndex, newIndex, collection }) => {
  switch (collection) {
    case 'Draggin' and Droppin' in Reacts': setGifs(arrayMove(Draggin' and Droppin' in Reacts, oldIndex, newIndex)); break;
    case 'newGifs': setNewGifs(arrayMove(newGifs, oldIndex, newIndex)); break;
  }
};

// ... in your render method ...
<sortablegifscontainer axis="x" onsortend="{onSortEnd}">
  {newGifs.map((Draggin' and Droppin' in React, i) => <sortablegif collection="newGifs" draggin and droppin in react="{Draggin'" index="{i}" key="{Draggin'"></sortablegif>)}
</sortablegifscontainer>

Remember to add the collection="Draggin' and Droppin' in Reacts" prop to the first list as well. The complete code is available on GitHub (link provided in the original text).

The above is the detailed content of Draggin' and Droppin' in React. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
A Little Reminder That Pseudo Elements are Children, Kinda.A Little Reminder That Pseudo Elements are Children, Kinda.Apr 19, 2025 am 11:39 AM

Here's a container with some child elements:

Menus with 'Dynamic Hit Areas'Menus with 'Dynamic Hit Areas'Apr 19, 2025 am 11:37 AM

Flyout menus! The second you need to implement a menu that uses a hover event to display more menu items, you're in tricky territory. For one, they should

Improving Video Accessibility with WebVTTImproving Video Accessibility with WebVTTApr 19, 2025 am 11:27 AM

"The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect."- Tim Berners-Lee

Weekly Platform News: CSS ::marker pseudo-element, pre-rendering web components, adding Webmention to your siteWeekly Platform News: CSS ::marker pseudo-element, pre-rendering web components, adding Webmention to your siteApr 19, 2025 am 11:25 AM

In this week's roundup: datepickers are giving keyboard users headaches, a new web component compiler that helps fight FOUC, we finally get our hands on styling list item markers, and four steps to getting webmentions on your site.

Making width and flexible items play nice togetherMaking width and flexible items play nice togetherApr 19, 2025 am 11:23 AM

The short answer: flex-shrink and flex-basis are probably what you’re lookin’ for.

Position Sticky and Table HeadersPosition Sticky and Table HeadersApr 19, 2025 am 11:21 AM

You can't position: sticky; a

Weekly Platform News: HTML Inspection in Search Console, Global Scope of Scripts, Babel env Adds defaults QueryWeekly Platform News: HTML Inspection in Search Console, Global Scope of Scripts, Babel env Adds defaults QueryApr 19, 2025 am 11:18 AM

In this week's look around the world of web platform news, Google Search Console makes it easier to view crawled markup, we learn that custom properties

IndieWeb and WebmentionsIndieWeb and WebmentionsApr 19, 2025 am 11:16 AM

The IndieWeb is a thing! They've got a conference coming up and everything. The New Yorker is even writing about it:

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment