draggable reactjs code example
Example 1: react drag and drop
Install:
yarn add react-dnd react-dnd-html5-backend
Example:
import React from 'react'
import { useDrag } from 'react-dnd'
import { ItemTypes } from './Constants'
export default function Card({ isDragging, text }) {
const [{ opacity }, dragRef] = useDrag({
item: { type: ItemTypes.CARD, text },
collect: monitor => ({
opacity: monitor.isDragging() ? 0.5 : 1,
}),
})
return (
<div ref={dragRef} style={{ opacity }}>
{text}
</div>
)
}
Example 2: react-draggable
<Draggable> <div>I can now be moved around!</div></Draggable>