Home Web Front-end CSS Tutorial Draggin' and Droppin' in React

Draggin' and Droppin' in React

Apr 17, 2025 am 11:52 AM

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
Copy after login

Install necessary packages:

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

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; }
Copy after login

Define GIF data using useState:

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

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;
Copy after login

Implementing Drag and Drop:

Import necessary HOCs and arrayMove:

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

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>);
Copy after login

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>
Copy after login

Multiple Lists:

Add a second GIF array:

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

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>
Copy after login

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 of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

See all articles