From e22217fe8b9455e32695f88ca835e11442b0a937 Mon Sep 17 00:00:00 2001 From: Fabrizio Cucci Date: Tue, 18 Oct 2022 04:22:58 -0700 Subject: [PATCH] Add `useAnimatedValue` to public API Summary: Changelog: [General][Added] - Introduce `useAnimatedValue` hook to make it easier working with `Animated.Value`s in function components. Reviewed By: javache Differential Revision: D40434219 fbshipit-source-id: 3caf6ad98d11a534b8cc6816820bc1d125150380 --- Libraries/Animated/useAnimatedValue.d.ts | 15 ++++++++++++++ Libraries/Animated/useAnimatedValue.js | 25 ++++++++++++++++++++++++ index.js | 4 ++++ types/index.d.ts | 1 + 4 files changed, 45 insertions(+) create mode 100644 Libraries/Animated/useAnimatedValue.d.ts create mode 100644 Libraries/Animated/useAnimatedValue.js diff --git a/Libraries/Animated/useAnimatedValue.d.ts b/Libraries/Animated/useAnimatedValue.d.ts new file mode 100644 index 00000000000000..69e5a59fcf0951 --- /dev/null +++ b/Libraries/Animated/useAnimatedValue.d.ts @@ -0,0 +1,15 @@ +/** + * 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. + * + * @format + */ + +import type {Animated} from './Animated'; + +export function useAnimatedValue( + initialValue: number, + config?: Animated.AnimatedConfig, +): Animated.Value; diff --git a/Libraries/Animated/useAnimatedValue.js b/Libraries/Animated/useAnimatedValue.js new file mode 100644 index 00000000000000..0ee02303b45cfe --- /dev/null +++ b/Libraries/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; +} diff --git a/index.js b/index.js index 7ff16758c8b8bb..d2d8fd1316d69c 100644 --- a/index.js +++ b/index.js @@ -80,6 +80,7 @@ import typeof * as Systrace from './Libraries/Performance/Systrace'; import typeof ToastAndroid from './Libraries/Components/ToastAndroid/ToastAndroid'; import typeof * as TurboModuleRegistry from './Libraries/TurboModule/TurboModuleRegistry'; import typeof UIManager from './Libraries/ReactNative/UIManager'; +import typeof useAnimatedValue from './Libraries/Animated/useAnimatedValue'; import typeof useColorScheme from './Libraries/Utilities/useColorScheme'; import typeof useWindowDimensions from './Libraries/Utilities/useWindowDimensions'; import typeof UTFSequence from './Libraries/UTFSequence'; @@ -373,6 +374,9 @@ module.exports = { return require('./Libraries/ReactNative/RendererProxy') .unstable_batchedUpdates; }, + get useAnimatedValue(): useAnimatedValue { + return require('./Libraries/Animated/useAnimatedValue').default; + }, get useColorScheme(): useColorScheme { return require('./Libraries/Utilities/useColorScheme').default; }, diff --git a/types/index.d.ts b/types/index.d.ts index ad0f8e340468f8..4a392cd742f240 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -76,6 +76,7 @@ export * from '../Libraries/ActionSheetIOS/ActionSheetIOS'; export * from '../Libraries/Alert/Alert'; export * from '../Libraries/Animated/Animated'; export * from '../Libraries/Animated/Easing'; +export * from '../Libraries/Animated/useAnimatedValue'; export * from '../Libraries/AppState/AppState'; export * from '../Libraries/BatchedBridge/NativeModules'; export * from '../Libraries/Components/AccessibilityInfo/AccessibilityInfo';