MinJieLiu / react-photo-view

An exquisite React photo preview component.
https://react-photo-view.vercel.app
Apache License 2.0
1.56k stars 130 forks source link

Ref conflict with other drag library #149

Closed Laxy317 closed 1 year ago

Laxy317 commented 1 year ago

I'm using both react-photo-view and @dnd-kit/sortable on the same image, such as :

const { attributes, listeners, setNodeRef, transform, transition } =
    useSortable({ id: src })

return <PhotoView src={src} >
      <image src={src} ref={setNodeRef} {...listeners}>
    </PhotoView>

Both of them need to set ref, which cause the drag library invalid. Although I can add a div to fix it, but i think there will be a better solution, such as using react-merge-refs

MinJieLiu commented 1 year ago

In fact, we can use the useImperativeHandle API, welcome pr

Laxy317 commented 1 year ago

In fact, we can use the useImperativeHandle API, welcome pr

I don't think useImperativeHandle is useful. The problem is caused by

// PhotoView.tsx
const originRef = useRef<HTMLElement>(null);

if (children) {
    return Children.only(cloneElement(children, { ...eventListeners, ref: originRef }));
  }

The originRef will reset the user settings, there doesn't seem to be a way to fix it with useImperativeHandle.

MinJieLiu commented 1 year ago
useImperativeHandle(children?.ref, () => originRef.current);

It works fine. I didn't test it carefully and if there were no bugs I released the version

Laxy317 commented 1 year ago

OK,thanks