v2 How does one use color interpolation and withTiming hook
See original GitHub issueDescription
I am using Android Studio’s adb to test my app.
As per code, when the button is clicked, i would like to change the backgroundColor
of the View “interpolate-ly” from tomato
to teal
.
- Using the code directly below will not change the color, but when Fast Reload is initiated from my code editor, the color did change to
teal
. - Using the commented
backgroundColorInterpolation
tostyle={[anyOtherStyle, backgroundColorInterpolation]}
will crash my app upon entering the screen
Is that not how it works or has i been implementing them the wrong way?
Code
import { Button, } from 'react-native';
import Animated ,{
useAnimatedStyle,
useSharedValue,
withTiming,
interpolateColors
} from 'react-native-reanimated';
function testScreen(){
const colorAnim = useSharedValue(0)
const backgroundColorInterpolation = interpolateColors(colorAnimDerivedValue.value, {
inputRange:[0,15],
outputColorRange:["tomato","teal"]
})
// tried to use the one below, but app would crash without throwing any error
// const backgroundColorInterpolation = useAnimatedStyle(() => {
// const interpolation = interpolateColors(colorAnim.value, {
// inputRange:[0,15],
// outputColorRange:["tomato","teal"]
// })
// return {
// backgroundColor: interpolation
// };
// });
return (
<Animated.View style={{ flex:1, backgroundColor:backgroundColorInterpolation}}>
<Button
onPress={() => {
colorAnim.value = withTiming(15, {
duration: 1000,
});
}
}/>
</Animated.View>
);
};
export default testScreen
Package versions
- React: 16.13.1
- React Native:0.63.0
- React Native Reanimated:^2.0.0-alpha.5
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:10 (2 by maintainers)
Top Results From Across the Web
Interpolate Colors like a pro with React Native Reanimated 2
Today we are going to understand the concept behind the interpolateColor function from the react-native-reanimated (v2) package.
Read more >Interpolate Colors like a pro with React Native Reanimated 2 ...
In this tutorial we'll learn the concept behind the interpolateColor function from React Native Reanimated. To improve the learning ...
Read more >Reanimated 2 - the new approach to creating animations in ...
I encourage you to start using Reanimated 2 in your React Native projects ... The hook accepts an object where we can configure...
Read more >Shared Values | React Native Reanimated - Software Mansion
Shared Values are among the fundamental concepts behind Reanimated 2.0. ... In order to create a Shared Value reference, you should use useSharedValue...
Read more >How to Make Performant Animations in React Native using ...
Why should you use Reanimated to make smooth and quick animations and how to do it? ... 1. Benefits of React Native Reanimated...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi there, in order to pass some animated values to the styles, you need to use
useAnimatedStyle
hook. Passing values directly is no longer supported with Reanimated v2 API (You can still use Reanimated v1 with new Value and all the cond, set, add etc though).This particular example @atcode-soft provided is not working because the provided interpolateColor API is only compatible with Reanimated v1.
Unfortunately, there is no API for that in the core, but you can use the one react-native-redash provides.
useAnimatedStyle has to return an object, found undefined instead.