Skip to content

Commit

Permalink
Add useAnimatedValue to public API
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabriziocucci authored and OlimpiaZurek committed May 22, 2023
1 parent 37c4069 commit ce56203
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Libraries/Animated/useAnimatedValue.d.ts
Original file line number Diff line number Diff line change
@@ -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;
25 changes: 25 additions & 0 deletions Libraries/Animated/useAnimatedValue.js
Original file line number Diff line number Diff line change
@@ -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;
}
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
},
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit ce56203

Please sign in to comment.