File tree Expand file tree Collapse file tree 1 file changed +22
-15
lines changed
packages/core/src/data-editor Expand file tree Collapse file tree 1 file changed +22
-15
lines changed Original file line number Diff line number Diff line change @@ -3,25 +3,32 @@ import type { VisibleRegion } from "./visible-region.js";
33import type { DataEditorCoreProps } from "../index.js" ;
44import { useStateWithReactiveInput } from "../common/utils.js" ;
55
6- function useCallbackRef < T > ( initialValue : T , callback : ( newVal : T ) => void ) {
7- const realRef = React . useRef < T > ( initialValue ) ;
8- const cbRef = React . useRef ( callback ) ;
9- cbRef . current = callback ;
10-
11- return React . useMemo (
12- ( ) => ( {
6+ // shamelessly stolen and modified from: https://github.com/theKashey/use-callback-ref
7+ // MIT License https://github.com/theKashey/use-callback-ref/tree/master?tab=MIT-1-ov-file#readme
8+ function useCallbackRef < T > (
9+ initialValue : T | null ,
10+ callback : ( newValue : T | null , lastValue : T | null ) => void
11+ ) : React . MutableRefObject < T | null > {
12+ const [ ref ] = React . useState ( ( ) => ( {
13+ value : initialValue ,
14+ callback,
15+ facade : {
1316 get current ( ) {
14- return realRef . current ;
17+ return ref . value ;
1518 } ,
16- set current ( value : T ) {
17- if ( realRef . current !== value ) {
18- realRef . current = value ;
19- cbRef . current ( value ) ;
19+ set current ( value ) {
20+ const last = ref . value ;
21+
22+ if ( last !== value ) {
23+ ref . value = value ;
24+ ref . callback ( value , last ) ;
2025 }
2126 } ,
22- } ) ,
23- [ ]
24- ) ;
27+ } ,
28+ } ) ) ;
29+ ref . callback = callback ;
30+
31+ return ref . facade ;
2532}
2633
2734export function useInitialScrollOffset (
You can’t perform that action at this time.
0 commit comments