Skip to content

Commit 0c03d2f

Browse files
Add useScrollToTop offset Snack
1 parent 82ac431 commit 0c03d2f

4 files changed

Lines changed: 116 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import * as React from 'react';
2+
import { View, ScrollView, Image } from 'react-native';
3+
import { NavigationContainer, useScrollToTop } from '@react-navigation/native';
4+
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
5+
6+
function Albums() {
7+
const ref = React.useRef(null);
8+
9+
useScrollToTop(
10+
React.useRef({
11+
scrollToTop: () => ref.current?.scrollTo({ y: 100 }),
12+
})
13+
);
14+
15+
return (
16+
<ScrollView ref={ref}>
17+
<Image
18+
source={{ uri: 'https://facebook.github.io/react/logo-og.png' }}
19+
style={{ width: 400, height: 400 }}
20+
key="1"
21+
/>
22+
<Image
23+
source={{ uri: 'https://facebook.github.io/react/logo-og.png' }}
24+
style={{ width: 400, height: 400 }}
25+
key="2"
26+
/>
27+
<Image
28+
source={{ uri: 'https://facebook.github.io/react/logo-og.png' }}
29+
style={{ width: 400, height: 400 }}
30+
key="3"
31+
/>
32+
<Image
33+
source={{ uri: 'https://facebook.github.io/react/logo-og.png' }}
34+
style={{ width: 400, height: 400 }}
35+
key="4"
36+
/>
37+
</ScrollView>
38+
);
39+
}
40+
41+
function HomeScreen() {
42+
return <View />;
43+
}
44+
45+
const Tab = createBottomTabNavigator();
46+
47+
export default function App() {
48+
return (
49+
<NavigationContainer>
50+
<Tab.Navigator>
51+
<Tab.Screen name="Home" component={HomeScreen} />
52+
<Tab.Screen name="Albums" component={Albums} />
53+
</Tab.Navigator>
54+
</NavigationContainer>
55+
);
56+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import * as React from 'react';
2+
import { View, ScrollView, Image } from 'react-native';
3+
import { NavigationContainer, useScrollToTop } from '@react-navigation/native';
4+
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
5+
6+
function Albums() {
7+
const ref = React.useRef(null);
8+
9+
useScrollToTop(
10+
React.useRef({
11+
scrollToTop: () => ref.current?.scrollTo({ y: 100 }),
12+
})
13+
);
14+
15+
return (
16+
<ScrollView ref={ref}>
17+
<Image
18+
source={{ uri: 'https://facebook.github.io/react/logo-og.png' }}
19+
style={{ width: 400, height: 400 }}
20+
key="1"
21+
/>
22+
<Image
23+
source={{ uri: 'https://facebook.github.io/react/logo-og.png' }}
24+
style={{ width: 400, height: 400 }}
25+
key="2"
26+
/>
27+
<Image
28+
source={{ uri: 'https://facebook.github.io/react/logo-og.png' }}
29+
style={{ width: 400, height: 400 }}
30+
key="3"
31+
/>
32+
<Image
33+
source={{ uri: 'https://facebook.github.io/react/logo-og.png' }}
34+
style={{ width: 400, height: 400 }}
35+
key="4"
36+
/>
37+
</ScrollView>
38+
);
39+
}
40+
41+
function HomeScreen() {
42+
return <View />;
43+
}
44+
45+
const Tab = createBottomTabNavigator();
46+
47+
export default function App() {
48+
return (
49+
<NavigationContainer>
50+
<Tab.Navigator>
51+
<Tab.Screen name="Home" component={HomeScreen} />
52+
<Tab.Screen name="Albums" component={Albums} />
53+
</Tab.Navigator>
54+
</NavigationContainer>
55+
);
56+
}

versioned_docs/version-6.x/use-scroll-to-top.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ export default function (props) {
5151

5252
If you require offset to scroll position you can wrap and decorate passed reference:
5353

54+
<samp id="use-scroll-to-top-offset" />
55+
5456
```js
5557
import * as React from 'react';
5658
import { ScrollView } from 'react-native';

versioned_docs/version-7.x/use-scroll-to-top.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ export default function (props) {
5151

5252
If you require offset to scroll position you can wrap and decorate passed reference:
5353

54+
<samp id="use-scroll-to-top-offset" />
55+
5456
```js
5557
import * as React from 'react';
5658
import { ScrollView } from 'react-native';

0 commit comments

Comments
 (0)