Skip to content

Commit

Permalink
Merge pull request #1494 from dinkinflickaa/dinkinflicka/optimize-rer…
Browse files Browse the repository at this point in the history
…enders-on-click

Avoid re-rendering children on click event
  • Loading branch information
clauderic authored Nov 23, 2024
2 parents 9c078b4 + b3de0f7 commit 8919eb1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
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 @@ -372,6 +373,8 @@ export const DndContext = memo(function DndContext({
active: id,
});
dispatchMonitorEvent({type: 'onDragStart', event});
setActiveSensor(activeSensorRef.current);
setActivatorEvent(activatorEvent);
});
},
onMove(coordinates) {
Expand All @@ -384,10 +387,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 @@ -423,6 +423,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

0 comments on commit 8919eb1

Please sign in to comment.