Skip to content

Commit

Permalink
Fix needless disabled action dispatches
Browse files Browse the repository at this point in the history
The `useDroppable` hook was needlessly dispatching `SetDroppableDisabled` actions even if the `disabled` property had not changed since the droppable was registered.
  • Loading branch information
clauderic committed Mar 17, 2022
1 parent 30bbd12 commit 77e3d44
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/droppable-disabled-dispatch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@dnd-kit/core': minor
---

Fixed an issue with `useDroppable` hook needlessly dispatching `SetDroppableDisabled` actions even if the `disabled` property had not changed since registering the droppable.
13 changes: 7 additions & 6 deletions packages/core/src/hooks/useDroppable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export function useDroppable({
const {active, dispatch, over, measureDroppableContainers} = useContext(
InternalContext
);
const previous = useRef({disabled});
const resizeObserverConnected = useRef(false);
const rect = useRef<ClientRect | null>(null);
const callbackId = useRef<NodeJS.Timeout | null>(null);
Expand Down Expand Up @@ -140,18 +141,18 @@ export function useDroppable({
[id]
);

useEffect(
() => {
useEffect(() => {
if (disabled !== previous.current.disabled) {
dispatch({
type: Action.SetDroppableDisabled,
id,
key,
disabled,
});
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[disabled]
);

previous.current.disabled = disabled;
}
}, [id, key, disabled, dispatch]);

return {
active,
Expand Down

0 comments on commit 77e3d44

Please sign in to comment.