Skip to content

Commit

Permalink
Minor polishing and code style
Browse files Browse the repository at this point in the history
  • Loading branch information
clauderic committed Jan 10, 2022
1 parent 1ade2f3 commit eb6d4c9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .changeset/use-latest-value.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'@dnd-kit/utilities': minor
---

Introduced the `useLatestValue` hook, which returns a ref that holds the latest value of a given argument.
Introduced the `useLatestValue` hook, which returns a ref that holds the latest value of a given argument. Optionally, the second argument can be used to customize the dependencies passed to the effect.
62 changes: 30 additions & 32 deletions packages/core/src/components/DndContext/DndContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
add,
getEventCoordinates,
Transform,
useLatestValue,
useIsomorphicLayoutEffect,
useUniqueId,
} from '@dnd-kit/utilities';
Expand Down Expand Up @@ -183,11 +184,12 @@ export const DndContext = memo(function DndContext({
const activeRef = useRef<UniqueIdentifier | null>(null);
const [activeSensor, setActiveSensor] = useState<SensorInstance | null>(null);
const [activatorEvent, setActivatorEvent] = useState<Event | null>(null);
const latestProps = useRef(props);
const latestProps = useLatestValue(props, Object.values(props));
const draggableDescribedById = useUniqueId(`DndDescribedBy`, id);
const enabledDroppableContainers = useMemo(() => {
return droppableContainers.getEnabled();
}, [droppableContainers]);
const enabledDroppableContainers = useMemo(
() => droppableContainers.getEnabled(),
[droppableContainers]
);
const {
droppableRects,
measureDroppableContainers,
Expand Down Expand Up @@ -225,7 +227,6 @@ export const DndContext = memo(function DndContext({
const overNode = droppableContainers.getNodeFor(
sensorContext.current.over?.id
);

const dragOverlay = useDragOverlayMeasuring({
measure: measuring?.dragOverlay?.measure,
});
Expand Down Expand Up @@ -424,7 +425,8 @@ export const DndContext = memo(function DndContext({
};
}
},
[dispatch, draggableNodes]
// eslint-disable-next-line react-hooks/exhaustive-deps
[draggableNodes]
);

const bindActivatorToSensorInstantiator = useCallback(
Expand Down Expand Up @@ -465,14 +467,6 @@ export const DndContext = memo(function DndContext({

useSensorSetup(sensors);

useIsomorphicLayoutEffect(
() => {
latestProps.current = props;
},
// eslint-disable-next-line react-hooks/exhaustive-deps
Object.values(props)
);

useEffect(() => {
if (activeId != null) {
setIsDragging(true);
Expand All @@ -489,27 +483,31 @@ export const DndContext = memo(function DndContext({
}
}, [activeNodeRect, active]);

useEffect(() => {
const {onDragMove} = latestProps.current;
const {active, collisions, over} = sensorContext.current;
useEffect(
() => {
const {onDragMove} = latestProps.current;
const {active, collisions, over} = sensorContext.current;

if (!active) {
return;
}
if (!active) {
return;
}

const event: DragMoveEvent = {
active,
collisions,
delta: {
x: scrollAdjustedTranslate.x,
y: scrollAdjustedTranslate.y,
},
over,
};
const event: DragMoveEvent = {
active,
collisions,
delta: {
x: scrollAdjustedTranslate.x,
y: scrollAdjustedTranslate.y,
},
over,
};

setMonitorState({type: Action.DragMove, event});
onDragMove?.(event);
}, [scrollAdjustedTranslate.x, scrollAdjustedTranslate.y]);
setMonitorState({type: Action.DragMove, event});
onDragMove?.(event);
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[scrollAdjustedTranslate.x, scrollAdjustedTranslate.y]
);

useEffect(
() => {
Expand Down
13 changes: 2 additions & 11 deletions packages/core/src/hooks/utilities/useDragOverlayMeasuring.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import {useMemo, useCallback, useState, useRef} from 'react';
import {
isHTMLElement,
useIsomorphicLayoutEffect,
useNodeRef,
} from '@dnd-kit/utilities';
import {useMemo, useCallback, useState} from 'react';
import {isHTMLElement, useNodeRef} from '@dnd-kit/utilities';

import {getMeasurableNode} from '../../utilities/nodes';
import {getClientRect} from '../../utilities/rect';
Expand All @@ -18,7 +14,6 @@ export function useDragOverlayMeasuring({
measure = getClientRect,
}: Arguments): PublicContextDescriptor['dragOverlay'] {
const [rect, setRect] = useState<ClientRect | null>(null);
const measureRef = useRef(measure);
const handleResize = useCallback(
(entries: ResizeObserverEntry[]) => {
for (const {target} of entries) {
Expand Down Expand Up @@ -55,10 +50,6 @@ export function useDragOverlayMeasuring({
);
const [nodeRef, setRef] = useNodeRef(handleNodeChange);

useIsomorphicLayoutEffect(() => {
measureRef.current = measure;
}, [measure]);

return useMemo(
() => ({
nodeRef,
Expand Down
7 changes: 5 additions & 2 deletions packages/utilities/src/hooks/useLatestValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import {useRef} from 'react';

import {useIsomorphicLayoutEffect} from './useIsomorphicLayoutEffect';

export function useLatestValue<T extends any>(value: T) {
export function useLatestValue<T extends any>(
value: T,
dependencies = [value]
) {
const valueRef = useRef<T>(value);

useIsomorphicLayoutEffect(() => {
if (valueRef.current !== value) {
valueRef.current = value;
}
}, [value]);
}, dependencies);

return valueRef;
}

0 comments on commit eb6d4c9

Please sign in to comment.