ReactNativeのScrollViewで、scroll to topする
ScrollViewには、scrollTo
というpropsが用意されているので、ref経由で呼び出せばよい。
refとそれを使った関数を用意して、
const scrollViewRef = useRef<ScrollView>(null); const goToTop = () => { if (scrollViewRef.current) { scrollViewRef.current.scrollTo({ x: 0, y: 0, animated: false }); } };
こんな感じで使う
<ScrollView ref={scrollViewRef}> {content} </ContentText> <Button onPress={goToTop}> <Text>Go to Top!</Text> </Button>
enjoy