You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
26 lines
977 B
26 lines
977 B
import { shallowEqual } from '@react-dnd/shallowequal';
|
|
import { useState, useCallback } from 'react';
|
|
import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';
|
|
/**
|
|
*
|
|
* @param monitor The monitor to collect state from
|
|
* @param collect The collecting function
|
|
* @param onUpdate A method to invoke when updates occur
|
|
*/
|
|
export function useCollector(monitor, collect, onUpdate) {
|
|
const [collected, setCollected] = useState(() => collect(monitor));
|
|
const updateCollected = useCallback(() => {
|
|
const nextValue = collect(monitor);
|
|
if (!shallowEqual(collected, nextValue)) {
|
|
setCollected(nextValue);
|
|
if (onUpdate) {
|
|
onUpdate();
|
|
}
|
|
}
|
|
}, [collected, monitor, onUpdate]);
|
|
// update the collected properties after the first render
|
|
// and the components are attached to dnd-core
|
|
useIsomorphicLayoutEffect(updateCollected, []);
|
|
return [collected, updateCollected];
|
|
}
|