Issue with React-Draggable Library: Elements Shifting After Dragging

I’m encountering an issue with the React-Draggable library in my project. I’ve implemented draggable elements using this library. Here’s a summary of the problem:

After dragging an element, I save its new position in the database. When the app is refreshed, the elements are correctly positioned based on their saved positions. However, when dragging one of the elements, the dragged element’s position offset is applied on top of the new position, causing the element to shift suddenly.

The issue occurs when using either

  • the “left” and “top” CSS properties in DateTicket component with absolute positioning
  • the “positionOffset” prop inside the Draggable component
  • the “position” prop inside the Draggable component

however, with the “position” prop, the element shifts and returns to the correct position rapidly, it looks like a glitch.
The problem arises specifically after dragging and onStop event.

// DateTicket component

const DateTicket = ({ ticket, onStop }) => {
  const draggableRef = useRef(null);

  return (
    <Draggable
     // positionOffset={{ x: `${ticket.xPosition}px`, y: `${ticket.yPosition}px` }}
      position={{ x: ticket.xPosition, y: ticket.yPosition }}

      onStart={(e) => draggableRef.current = e.currentTarget }
      onStop={(e) => onStop(e, ticket) }
      nodeRef={draggableRef}
    >
      <div
        className="dateTicket"
        ref={draggableRef}
        // style={{ left: `${ticket.xPosition}px`, top: `${ticket.yPosition}px`}}
      >
        <span>{ticket.year}</span>
        <span>{ticket.day}</span>
        <span>{ticket.month}</span>
      </div>
    </Draggable>
// App.jsx file

function App() {

  const draggableRef = useRef(null);

  // call of queries
  const [getDateTickets, { data, error, refetch }] = useLazyQuery(
    GET_DATE_TICKETS,
    { fetchPolicy: "network-only" }
  );
  const [updateTicket, updateResponse] = useMutation(UPDATE_DATE_TICKET);

  // get all elements when mounting the app
  useEffect(() => {
    getDateTickets();
    refetch();
  }, [updateResponse?.loading]);

  // ------------------------- draggable onStop event ------------------------
  const onStop = (e, element) => {
      updateTicket({
        variables: {
          updateDateTicketId: element.id,
          data: {
            day: element.day,
            month: element.month,
            year: element.year,
            xPosition: draggableRef.current.getBoundingClientRect().x,
            yPosition: draggableRef.current.getBoundingClientRect().y,
          },
        },
      });
    refetch();
  };

  useEffect(() => {
    if (!updateResponse.error && updateResponse.data?.updateDateTicket) refetch();
  }, [updateResponse.data, updateResponse.error]);
 
  return (
    <div className="app">
        {!error?.getDateTickets &&
        !!data?.getDateTickets.length &&
        data.getDateTickets.map((ticket) => (
          <DateTicket
            key={ticket.id}
            ticket={ticket}
            onStop={onStop}
          />
        )}
    </div>
  );
}

Not using the css properties nor the “position” props, elements are placed properly after dragging but on app mounting or on refresh, all the elements are stacked at position (0,0).
thus the goal is to correctly place the elements on app mounting and onStop (after dragging) without sudden shifts in elements positions.

Any insights or suggestions would be greatly appreciated. Thank you!

New contributor

Amani_NFZ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật