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";
3
3
import type { DataEditorCoreProps } from "../index.js" ;
4
4
import { useStateWithReactiveInput } from "../common/utils.js" ;
5
5
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 : {
13
16
get current ( ) {
14
- return realRef . current ;
17
+ return ref . value ;
15
18
} ,
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 ) ;
20
25
}
21
26
} ,
22
- } ) ,
23
- [ ]
24
- ) ;
27
+ } ,
28
+ } ) ) ;
29
+ ref . callback = callback ;
30
+
31
+ return ref . facade ;
25
32
}
26
33
27
34
export function useInitialScrollOffset (
You can’t perform that action at this time.
0 commit comments