From 39f37d78d62654fd274824fdcf5054a780fd99eb Mon Sep 17 00:00:00 2001 From: D N <4661784+retyui@users.noreply.github.com> Date: Thu, 28 Nov 2024 19:51:37 +0100 Subject: [PATCH] feat: Add a missing `useAnimatedValue` hook Issue: https://github.com/necolas/react-native-web/issues/2707 --- .../src/exports/Animated/useAnimatedValue.js | 13 ++++++++++ packages/react-native-web/src/index.js | 1 + .../react-native/Animated/useAnimatedValue.js | 25 +++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 packages/react-native-web/src/exports/Animated/useAnimatedValue.js create mode 100644 packages/react-native-web/src/vendor/react-native/Animated/useAnimatedValue.js diff --git a/packages/react-native-web/src/exports/Animated/useAnimatedValue.js b/packages/react-native-web/src/exports/Animated/useAnimatedValue.js new file mode 100644 index 000000000..a127c4203 --- /dev/null +++ b/packages/react-native-web/src/exports/Animated/useAnimatedValue.js @@ -0,0 +1,13 @@ +/** + * Copyright (c) Davyd Narciso. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + */ + +'use client'; + +import useAnimatedValue from '../../vendor/react-native/Animated/useAnimatedValue'; +export default useAnimatedValue; diff --git a/packages/react-native-web/src/index.js b/packages/react-native-web/src/index.js index f1a6f23fc..f7e27f7b4 100644 --- a/packages/react-native-web/src/index.js +++ b/packages/react-native-web/src/index.js @@ -68,3 +68,4 @@ export { default as DeviceEventEmitter } from './exports/DeviceEventEmitter'; export { default as useColorScheme } from './exports/useColorScheme'; export { default as useLocaleContext } from './exports/useLocaleContext'; export { default as useWindowDimensions } from './exports/useWindowDimensions'; +export { default as useAnimatedValue } from './exports/Animated/useAnimatedValue'; \ No newline at end of file diff --git a/packages/react-native-web/src/vendor/react-native/Animated/useAnimatedValue.js b/packages/react-native-web/src/vendor/react-native/Animated/useAnimatedValue.js new file mode 100644 index 000000000..fecfe099b --- /dev/null +++ b/packages/react-native-web/src/vendor/react-native/Animated/useAnimatedValue.js @@ -0,0 +1,25 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict-local + * @format + */ + +import type {AnimatedValueConfig} from './nodes/AnimatedValue'; + +import Animated from './Animated'; +import {useRef} from 'react'; + +export default function useAnimatedValue( + initialValue: number, + config?: ?AnimatedValueConfig, +): Animated.Value { + const ref = useRef(null); + if (ref.current == null) { + ref.current = new Animated.Value(initialValue, config); + } + return ref.current; +} \ No newline at end of file