Replies: 1 comment
-
Here's how I got it working to be reactive with the state in the end: export const useDragAndDropWithZustand = (
id: string,
dragParent: MutableRefObject<HTMLDivElement | null>,
) => {
const { segments, setSegments } = useStore(
(state: Store) => ({
segments: state.segments,
setSegments: state.setSegments,
}),
);
const setSegmentsWrapper = useCallback(
(newSegmentsOrUpdater: string[] | ((prev: string[]) => string[])) => {
if (typeof newSegmentsOrUpdater === "function") {
setSegments(id, newSegmentsOrUpdater(segments!));
} else {
setSegments(subtopicId, newSegmentsOrUpdater);
}
},
[segments, setSegments, id],
);
useEffect(() => {
if (!dragParent || !segments) return;
dragAndDrop({
parent: dragParent,
state: [segments, setSegmentsWrapper],
plugins: [animations()],
});
}, [segments, setSegmentsWrapper, dragParent]);
return { segments, setSegments: setSegmentsWrapper };
}; setSegments just takes the entire array with the new order and sets it to the segments state variable. Key was to wrap it in order to "mock" the dispatch type that dragAndDrop expects. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have managed to hack together a solution with the useDragAndDrop in a custom hook, using a custom plugin like this:
The dragAndDrop is supposed to work directly with state though, but I can't get it working with Zustand myself. Anyone else had any luck with this that can give some pointers?
Beta Was this translation helpful? Give feedback.
All reactions