I'm kinda new to RN and I'm having a hard time tracking down a "Maximum stack call exceeded". I understand that the error is related to some kind of infinite loop going on in my code but I can't find it.
Code
EventScreen:
import Animated, { interpolate, Extrapolate, Value } from "react-native-reanimated";
export default class EventScreen extends React.Component<EventScreenProps, EventScreenState> {
render() {
const animatedValueRef = new Value(0);
const height = interpolate(animatedValueRef, {
inputRange: [-MAX_HEADER_HEIGHT, 0],
outputRange: [0, MAX_HEADER_HEIGHT],
extrapolate: Extrapolate.CLAMP
});
return (
<SafeAreaView style={style.screenContainer}>
<CollapsibleHeaderBar backgroundImage={event?.flyer} animatedValueRef={animatedValueRef} >
<TouchableOpacity style={style.shareButton}>
<Icon2 name="ios-share-alt" size={24} style={style.shareButtonIcon} />
<Text style={style.shareButtonText}>CONDIVIDI</Text>
</TouchableOpacity>
</CollapsibleHeaderBar>
<Animated.ScrollView
style={style.flexOne}
onScroll={onScroll({ y: animatedValueRef })}
showsVerticalScrollIndicator={false}
scrollEventThrottle={1}>
<View style={style.cover}>
<Animated.View style={[style.gradient, { height }]} />
<LinearGradient style={style.flexOne} colors={["transparent", "rgba(0, 0, 0, 0.2)", Colors.appBackgroundColorValue]} />
</View>
...
</Animated.ScrollView>
...
</SafeAreaView>
)
}
}
CollapsibleHeaderBar:
import { interpolate, Extrapolate, color } from "react-native-reanimated";
export default class CollapsibleHeaderBar extends React.PureComponent<CollapsibleHeaderBarProps> {
render() {
const { backgroundImage, collapsedText, animatedValueRef } = this.props;
const opacity = interpolate(animatedValueRef, {
inputRange: [-50, 0, HEADER_DELTA + 60],
outputRange: [0, 0, 1],
extrapolate: Extrapolate.CLAMP,
});
const textOpacity = interpolate(animatedValueRef, {
inputRange: [HEADER_DELTA - 30, HEADER_DELTA],
outputRange: [0, 1],
extrapolate: Extrapolate.CLAMP,
});
const backgroundColor = color(18, 18, 18, interpolate(animatedValueRef, {
inputRange: [MIN_HEADER_HEIGHT, HEADER_DELTA - 30],
outputRange: [0.3, 1],
extrapolate: Extrapolate.CLAMP,
}));
const borderBottomColor = color(65, 65, 65, interpolate(animatedValueRef, {
inputRange: [MIN_HEADER_HEIGHT, HEADER_DELTA - 30],
outputRange: [0.3, 1],
extrapolate: Extrapolate.CLAMP,
}));
return (
<View>
<Animated.View style={style.headerContainer}>
<Image style={style.backgroundImage} source={{ uri: backgroundImage }} />
<Animated.View style={[style.barOverlay, { opacity }]} />
</Animated.View>
<Animated.View style={[style.barAnimation, { backgroundColor, borderBottomColor }]}>
<View style={style.barContainer}>
<TouchableOpacity onPress={this._onBackPressed}>
<Icon name="chevron-left" size={24} style={style.goBackIcon} />
</TouchableOpacity>
<Animated.Text style={{ opacity: textOpacity }}>{collapsedText}</Animated.Text>
</View>
{this.props.children}
</Animated.View>
</View>
);
}
}
Error
What I've understood so far
Thanks for your help.
I've solved the problem. The Animated I was importing on the CollapsibleHeaderBar was not from the same library (reanimated) but was from react-native. Changing that fixed it.
I've also learned that you have to put the animated value inside the state, otherwise the animation won't work properly.