Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid re-rendering children on click event #1494

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/brave-kids-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@dnd-kit/core': patch
---

Improves performance by eliminating wasteful re-renders on every child item on click
9 changes: 5 additions & 4 deletions packages/core/src/components/DndContext/DndContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ export const DndContext = memo(function DndContext({
activeNodeRect
);

const activeSensorRef = useRef<SensorInstance | null>(null);
const instantiateSensor = useCallback(
(
event: React.SyntheticEvent,
Expand Down Expand Up @@ -371,6 +372,8 @@ export const DndContext = memo(function DndContext({
active: id,
});
dispatchMonitorEvent({type: 'onDragStart', event});
setActiveSensor(activeSensorRef.current);
setActivatorEvent(activatorEvent);
});
},
onMove(coordinates) {
Expand All @@ -383,10 +386,7 @@ export const DndContext = memo(function DndContext({
onCancel: createHandler(Action.DragCancel),
});

unstable_batchedUpdates(() => {
setActiveSensor(sensorInstance);
setActivatorEvent(event.nativeEvent);
});
activeSensorRef.current = sensorInstance;

function createHandler(type: Action.DragEnd | Action.DragCancel) {
return async function handler() {
Expand Down Expand Up @@ -422,6 +422,7 @@ export const DndContext = memo(function DndContext({
setOver(null);
setActiveSensor(null);
setActivatorEvent(null);
activeSensorRef.current = null;

const eventName =
type === Action.DragEnd ? 'onDragEnd' : 'onDragCancel';
Expand Down