Skip to content

Commit

Permalink
Expose useLatestValue hook
Browse files Browse the repository at this point in the history
  • Loading branch information
clauderic committed Jan 8, 2022
1 parent 02edd26 commit 528c67e
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/use-latest-value.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@dnd-kit/utilities': minor
---

Introduced the `useLatestValue` hook, which returns a ref that holds the latest value of a given argument.
9 changes: 3 additions & 6 deletions packages/core/src/hooks/useDraggable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@ import {createContext, useContext, useMemo} from 'react';
import {
Transform,
useNodeRef,
useUniqueId,
useIsomorphicLayoutEffect,
useLatestValue,
useUniqueId,
} from '@dnd-kit/utilities';

import {Context, Data} from '../store';
import {ActiveDraggableContext} from '../components/DndContext';
import {
useLatestValue,
useSyntheticListeners,
SyntheticListenerMap,
} from './utilities';
import {useSyntheticListeners, SyntheticListenerMap} from './utilities';

export interface UseDraggableArguments {
id: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/hooks/useDroppable.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {useCallback, useContext, useEffect, useMemo, useRef} from 'react';
import {
useIsomorphicLayoutEffect,
useLatestValue,
useNodeRef,
useUniqueId,
} from '@dnd-kit/utilities';

import {Context, Action, Data} from '../store';
import type {ClientRect, UniqueIdentifier} from '../types';
import {useLatestValue} from './utilities';

interface ResizeObserverConfig {
/** Whether the ResizeObserver should be disabled entirely */
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/hooks/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export {
export type {Options as AutoScrollOptions} from './useAutoScroller';
export {useCachedNode} from './useCachedNode';
export {useCombineActivators} from './useCombineActivators';
export {useLatestValue} from './useLatestValue';
export {
useDroppableMeasuring,
MeasuringFrequency,
Expand Down
14 changes: 0 additions & 14 deletions packages/core/src/hooks/utilities/useLatestValue.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/utilities/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export {useCombinedRefs} from './useCombinedRefs';
export {useIsomorphicLayoutEffect} from './useIsomorphicLayoutEffect';
export {useInterval} from './useInterval';
export {useLatestValue} from './useLatestValue';
export {useLazyMemo} from './useLazyMemo';
export {useNodeRef} from './useNodeRef';
export {useUniqueId} from './useUniqueId';
15 changes: 15 additions & 0 deletions packages/utilities/src/hooks/useLatestValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {useRef} from 'react';

import {useIsomorphicLayoutEffect} from './useIsomorphicLayoutEffect';

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

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

return valueRef;
}
1 change: 1 addition & 0 deletions packages/utilities/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export {
useCombinedRefs,
useIsomorphicLayoutEffect,
useInterval,
useLatestValue,
useLazyMemo,
useNodeRef,
useUniqueId,
Expand Down

0 comments on commit 528c67e

Please sign in to comment.