Skip to content

Commit e22217f

Browse files
fabriziocuccifacebook-github-bot
authored andcommitted
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
1 parent b5aec96 commit e22217f

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
*/
9+
10+
import type {Animated} from './Animated';
11+
12+
export function useAnimatedValue(
13+
initialValue: number,
14+
config?: Animated.AnimatedConfig,
15+
): Animated.Value;
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
*/
10+
11+
import type {AnimatedValueConfig} from './nodes/AnimatedValue';
12+
13+
import Animated from './Animated';
14+
import {useRef} from 'react';
15+
16+
export default function useAnimatedValue(
17+
initialValue: number,
18+
config?: ?AnimatedValueConfig,
19+
): Animated.Value {
20+
const ref = useRef(null);
21+
if (ref.current == null) {
22+
ref.current = new Animated.Value(initialValue, config);
23+
}
24+
return ref.current;
25+
}

index.js

+4
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ import typeof * as Systrace from './Libraries/Performance/Systrace';
8080
import typeof ToastAndroid from './Libraries/Components/ToastAndroid/ToastAndroid';
8181
import typeof * as TurboModuleRegistry from './Libraries/TurboModule/TurboModuleRegistry';
8282
import typeof UIManager from './Libraries/ReactNative/UIManager';
83+
import typeof useAnimatedValue from './Libraries/Animated/useAnimatedValue';
8384
import typeof useColorScheme from './Libraries/Utilities/useColorScheme';
8485
import typeof useWindowDimensions from './Libraries/Utilities/useWindowDimensions';
8586
import typeof UTFSequence from './Libraries/UTFSequence';
@@ -373,6 +374,9 @@ module.exports = {
373374
return require('./Libraries/ReactNative/RendererProxy')
374375
.unstable_batchedUpdates;
375376
},
377+
get useAnimatedValue(): useAnimatedValue {
378+
return require('./Libraries/Animated/useAnimatedValue').default;
379+
},
376380
get useColorScheme(): useColorScheme {
377381
return require('./Libraries/Utilities/useColorScheme').default;
378382
},

types/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export * from '../Libraries/ActionSheetIOS/ActionSheetIOS';
7676
export * from '../Libraries/Alert/Alert';
7777
export * from '../Libraries/Animated/Animated';
7878
export * from '../Libraries/Animated/Easing';
79+
export * from '../Libraries/Animated/useAnimatedValue';
7980
export * from '../Libraries/AppState/AppState';
8081
export * from '../Libraries/BatchedBridge/NativeModules';
8182
export * from '../Libraries/Components/AccessibilityInfo/AccessibilityInfo';

0 commit comments

Comments
 (0)