-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add useHandleRef as a lighter alternative for useAnimatedRef (#6500)
- Loading branch information
Showing
7 changed files
with
75 additions
and
54 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,39 @@ | ||
import {useState} from 'react' | ||
import {AnimatedRef, measure, MeasuredDimensions} from 'react-native-reanimated' | ||
|
||
export type HandleRef = { | ||
(node: any): void | ||
current: null | number | ||
} | ||
|
||
// This is a lighterweight alternative to `useAnimatedRef()` for imperative UI thread actions. | ||
// Render it like <View ref={ref} />, then pass `ref.current` to `measureHandle()` and such. | ||
export function useHandleRef(): HandleRef { | ||
return useState(() => { | ||
const ref = (node: any) => { | ||
if (node) { | ||
ref.current = | ||
node._nativeTag ?? | ||
node.__nativeTag ?? | ||
node.canonical?.nativeTag ?? | ||
null | ||
} else { | ||
ref.current = null | ||
} | ||
} | ||
ref.current = null | ||
return ref | ||
})[0] as HandleRef | ||
} | ||
|
||
// When using this version, you need to read ref.current on the JS thread, and pass it to UI. | ||
export function measureHandle( | ||
current: number | null, | ||
): MeasuredDimensions | null { | ||
'worklet' | ||
if (current !== null) { | ||
return measure((() => current) as AnimatedRef<any>) | ||
} else { | ||
return null | ||
} | ||
} |
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
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
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