Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

420
Views
How to make my react-dnd droppable component draggable too?

I have this dustbin component that accepts some numbers. But after accepting numbers, I want to drag that box too rather than picking numbers from the left and right side of my window. Here's my current Dustbin.js code:

import { memo } from 'react';
import { useDrop } from 'react-dnd';
const style = {
    height: '6rem',
    width: '6rem',
    marginRight: '5px',
    padding: '1rem',
    textAlign: 'center',
    fontSize: '1rem',
    lineHeight: 'normal',
};
export const Dustbin = memo(function Dustbin({ shouldAccept, accept, lastDroppedItem, onDrop, }) {
    const [{ isOver, canDrop }, drop] = useDrop({
        accept,
        drop: onDrop,
        collect: (monitor) => ({
            isOver: monitor.isOver(),
            canDrop: monitor.canDrop(),
        }),
    });
    const isActive = isOver && canDrop;
    let backgroundColor = '#CBD5E1';
    if (isActive) {
        backgroundColor = 'darkgreen';
    }
    else if (canDrop) {
        backgroundColor = 'darkkhaki'
    }
    return (<div ref={drop} className='flex justify-center items-center' role="Dustbin" style={{ ...style, backgroundColor }}>
        {
            lastDroppedItem && lastDroppedItem?.name == shouldAccept && `${shouldAccept}`}
    </div>);
});

Here's a picture of what my application looks like right now.

enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

To make ondrop components draggable, just put an ondrag next to ondrop like this:

  const [{ isDragging }, drag] = useDrag({
    type: 'box-drag',
    item: () => {
      return { number: YourBoxNumber };
    },
    collect: (monitor: any) => ({
      isDragging: monitor.isDragging(),
    }),
  });

then update your useDrop to accept the dragging type box-type you may need to write another usedrop with the dragging type box-type for this.

  const [{ handlerId }, drop] = useDrop({
    accept: 'box-drag',
    collect(monitor) {
      return {
        handlerId: monitor.getHandlerId(),
      };
    },
    hover(item: { number }, monitor) {
      // do something on hover
    },
    drop(item: { number }, monitor: DropTargetMonitor) {
      // do something when another box dropped on it.
    },
  });

After all

drag(drop(ref))

ref is the ref for your box (currently your ref is drop, after it's dragging the name shall be updated either).

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!