Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

430
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda