How to create a style without hook? #2276
-
Hi, I am using reanimated V2 on my project to build a carousel using const _renderItem = ({ item, index }) => {
const inputRange = [index - 2, index - 1, index];
const itemStyle = useAnimatedStyle(() => ({ // <== can't do this
transform: [
{
scaleY: interpolate(scrollX.value, inputRange, [0.2, 1, 0.2]),
},
],
}));
return (
<View style={{ width: ITEM_SIZE }}>
<Animated.View
style={[
itemStyle,
styles.itemDefaultStyle,
]}
>
<Text>{index - 1}</Text>
</Animated.View>
</View>
);
};
<AnimatedFlatList
...
renderItem={_renderItem}
/> I wonder if I can somehow create the animated style without using hooks so I can animate components on my renderItem function |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi, you can achieve both: follow the rules of eslint and performance improvements if you move it to a component: function ItemRenderer({ index, item, scrollY }) {
const inputRange = [index - 2, index - 1, index];
const itemStyle = useAnimatedStyle(() => ({ // <== can't do this
transform: [
{
scaleY: interpolate(scrollX.value, inputRange, [0.2, 1, 0.2]),
},
],
}));
return (
<View style={{ width: ITEM_SIZE }}>
<Animated.View
style={[
itemStyle,
styles.itemDefaultStyle,
]}
>
<Text>{index - 1}</Text>
</Animated.View>
</View>
);
};
} But also use useCallback to create a function which you would pass to Flatlist: const renderItem = useCallback(({ item, index }) => (
<ItemRenderer item={item} index={index} scrollY={scrollY} />
), [scrollY]); |
Beta Was this translation helpful? Give feedback.
Hi, you can achieve both: follow the rules of eslint and performance improvements if you move it to a component:
But also use …