@codeleap/hooks
Advanced tools
+8
-8
| { | ||
| "name": "@codeleap/hooks", | ||
| "version": "6.2.3", | ||
| "version": "6.3.0", | ||
| "main": "src/index.ts", | ||
@@ -12,6 +12,6 @@ "license": "UNLICENSED", | ||
| "devDependencies": { | ||
| "@codeleap/config": "6.2.3", | ||
| "@codeleap/types": "6.2.3", | ||
| "@codeleap/utils": "6.2.3", | ||
| "@codeleap/logger": "6.2.3", | ||
| "@codeleap/config": "6.3.0", | ||
| "@codeleap/types": "6.3.0", | ||
| "@codeleap/utils": "6.3.0", | ||
| "@codeleap/logger": "6.3.0", | ||
| "ts-node-dev": "1.1.8" | ||
@@ -23,5 +23,5 @@ }, | ||
| "peerDependencies": { | ||
| "@codeleap/types": "6.2.3", | ||
| "@codeleap/utils": "6.2.3", | ||
| "@codeleap/logger": "6.2.3", | ||
| "@codeleap/types": "6.3.0", | ||
| "@codeleap/utils": "6.3.0", | ||
| "@codeleap/logger": "6.3.0", | ||
| "axios": "^1.7.9", | ||
@@ -28,0 +28,0 @@ "typescript": "5.5.2", |
+0
-1
@@ -35,3 +35,2 @@ import { | ||
| export * from './useId' | ||
| export * from './useAnimatedState' | ||
| export * from './useDebounceCallback' | ||
@@ -38,0 +37,0 @@ export * from './useDerivedRef' |
| import { useState, useCallback } from 'react' | ||
| import { SharedValue, useSharedValue, useDerivedValue, runOnJS, isSharedValue } from 'react-native-reanimated' | ||
| /** | ||
| * Hook that synchronizes a React state with a Reanimated SharedValue. | ||
| * | ||
| * @example | ||
| * const [value, setValue, sharedValue] = useAnimatedState(0) | ||
| * setValue(10) // Updates both React state and SharedValue | ||
| */ | ||
| export function useAnimatedState<T>(initialValue: SharedValue<T> | T) { | ||
| const sharedValue = isSharedValue(initialValue) | ||
| ? initialValue | ||
| : useSharedValue<T>(initialValue) | ||
| const [value, _setValue] = useState<T>( | ||
| isSharedValue(initialValue) ? initialValue.value : initialValue, | ||
| ) | ||
| useDerivedValue(() => { | ||
| runOnJS(_setValue)(sharedValue.value) | ||
| }) | ||
| const setValue = useCallback((newValue: T) => { | ||
| sharedValue.value = newValue | ||
| if (isSharedValue(initialValue)) initialValue.value = newValue | ||
| _setValue(newValue) | ||
| }, []) | ||
| return [value, setValue, sharedValue] as const | ||
| } | ||
| /** | ||
| * Hook that extracts and synchronizes the value from a SharedValue. | ||
| * | ||
| * @example | ||
| * const value = useAnimatedValue(sharedValue) | ||
| */ | ||
| export function useAnimatedValue<T>(initialValue: SharedValue<T> | T): T { | ||
| const [value] = useAnimatedState(initialValue) | ||
| return value | ||
| } |
Sorry, the diff of this file is not supported yet
30179
-4.11%31
-3.12%934
-3.81%