-
-
Notifications
You must be signed in to change notification settings - Fork 651
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow dependencies to be passed to
useComputed
- Loading branch information
Showing
4 changed files
with
24 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@dnd-kit/react': patch | ||
--- | ||
|
||
Allow dependencies to be passed to `useComputed` hook. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
import {useMemo, useRef} from 'react'; | ||
import {computed} from '@dnd-kit/state'; | ||
|
||
import {useConstant} from './useConstant.ts'; | ||
import {useSignal} from './useSignal.ts'; | ||
|
||
export function useComputed<T = any>(compute: () => T, sync = false) { | ||
export function useComputed<T = any>( | ||
compute: () => T, | ||
dependencies: any[] = [], | ||
sync = false | ||
) { | ||
const $compute = useRef(compute); | ||
$compute.current = compute; | ||
|
||
return useSignal( | ||
useConstant(() => computed(compute)), | ||
useMemo(() => computed(() => $compute.current()), dependencies), | ||
sync | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,27 @@ | ||
import {useState} from 'react'; | ||
import {flushSync} from 'react-dom'; | ||
import {effect, signal, Signal} from '@dnd-kit/state'; | ||
import {effect, Signal} from '@dnd-kit/state'; | ||
|
||
import {useConstant} from './useConstant.ts'; | ||
import {useIsomorphicLayoutEffect} from './useIsomorphicLayoutEffect.ts'; | ||
|
||
/** Wrap the given value in a Signal if it isn't already one, and make changes trigger a re-render. */ | ||
export function useSignal<T = any>(signalOrValue: T, sync = false) { | ||
const sig = useConstant(() => | ||
signalOrValue instanceof Signal ? signalOrValue : signal(signalOrValue) | ||
) as T extends Signal ? T : Signal<T>; | ||
let val = sig.peek(); | ||
export function useSignal<T = any>(signal: Signal<T>, sync = false) { | ||
let val = signal.peek(); | ||
const update = useState(val)[1]; | ||
|
||
useIsomorphicLayoutEffect( | ||
() => | ||
effect(() => { | ||
if (val !== (val = sig.value)) { | ||
if (val !== (val = signal.value)) { | ||
if (sync) { | ||
flushSync(() => update(val)); | ||
} else { | ||
update(val); | ||
} | ||
} | ||
}), | ||
[sync] | ||
[signal, sync] | ||
); | ||
return sig; | ||
|
||
return signal; | ||
} |