I have a video of a draggable ball online. I have to implement this functionality in react js. The ball can be dragged and crossed over the line After you release the club, the club will not return to its original position.
I tried multiple codes and methods but none of them worked as desired. I need help creating a functionality like this in React, if anyone knows how to do it please help me.
const Ball = ({ color, onDragStart, onDragOver, onDrop }) => ( <div className="ball" draggable onDragStart={onDragStart} onDragOver={onDragOver} onDrop={onDrop} style={{ backgroundColor: color }} /> ); import React, { useState } from 'react'; import Ball from './Ball'; const colors = ['red', 'blue', 'green', 'yellow']; const Line = () => { const [balls, setBalls] = useState(colors); const onDragStart = (e, index) => { e.dataTransfer.setData('index', index); }; const onDragOver = (e) => { e.preventDefault(); }; const onDrop = (e, index) => { const droppedIndex = e.dataTransfer.getData('index'); const newBalls = [...balls]; newBalls.splice(droppedIndex, 1); newBalls.splice(index, 0, colors[droppedIndex]); setBalls(newBalls); }; return ( <div className="line"> {balls.map((color, index) => ( <Ball key={index} color={color} onDragStart={(e) => onDragStart(e, index)} onDragOver={onDragOver} onDrop={(e) => onDrop(e, index)} /> ))} </div> ); };
This code does not work.