Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add a missing useAnimatedValue hook #2740

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/react-native-web-examples/pages/animated/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import React, { useRef } from 'react';
import { Animated, Pressable, StyleSheet, Text, View } from 'react-native';
import React from 'react';
import { Animated, Pressable, StyleSheet, Text, View, useAnimatedValue } from 'react-native';
import Example from '../../shared/example';

export default function AnimatedPage() {
const anim = useRef(new Animated.Value(0));
const anim = useAnimatedValue(0);

const animateBox = () => {
Animated.timing(anim.current, {
Animated.timing(anim, {
toValue: 1,
duration: 1000,
useNativeDriver: false
}).start();
};

const transform = anim.current.interpolate({
const transform = anim.interpolate({
inputRange: [0, 1],
outputRange: ['rotate(0deg)', 'rotate(45deg)']
});
Expand Down
13 changes: 13 additions & 0 deletions packages/react-native-web/src/exports/Animated/useAnimatedValue.js
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions packages/react-native-web/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
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 | Animated.Value>(null);
if (ref.current == null) {
ref.current = new Animated.Value(initialValue, config);
}
return ref.current;
}