scroll to top on click react native code example
Example: react native scroll to top function componet
export const Parent : FC<ParentProps> = props => {
const scrollRef = useRef<ScrollView>();
const onFabPress = () => {
scrollRef.current?.scrollTo({
y : 0,
animated : true
});
}
return (
<View>
<ScrollView ref={scrollRef}>
{}
</ScrollView>
<FloatingButton onPress={onFabPress} />
</View>
);
}
export const FloatingButton : FC<FloatingButtonProps> = props => {
const { onPress } = props;
const onFabPress = () => {
onPress();
}
return (
<Fab position="bottomRight" onPress={onFabPress}>
<Icon name="arrow-round-up" />
</Fab>
);
}
if you are using flatlist
const scrollRef = useRef<Flatlist>();
const onFabPress = () => {
scrollRef.current?.scrollToOffset({
y : 0,
animated : true
});
}
scrollToOffset is flatlist fucntion