-
Notifications
You must be signed in to change notification settings - Fork 465
Expand file tree
/
Copy pathimperative.tsx
More file actions
89 lines (85 loc) · 2.46 KB
/
imperative.tsx
File metadata and controls
89 lines (85 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import React, { useRef, useState } from 'react';
import { StyleSheet, Text, View, Pressable } from 'react-native';
import PagerView from 'react-native-pager-view';
const PAGES = [
{ key: '1', title: 'Page 1', color: '#ff6b6b' },
{ key: '2', title: 'Page 2', color: '#4ecdc4' },
{ key: '3', title: 'Page 3', color: '#45b7d1' },
];
export default function ImperativeExample() {
const pagerRef = useRef<PagerView>(null);
const [scrollEnabled, setScrollEnabled] = useState(true);
return (
<View style={styles.container}>
<PagerView
ref={pagerRef}
style={styles.pager}
initialPage={0}
scrollEnabled={scrollEnabled}
>
{PAGES.map((page) => (
<View
key={page.key}
style={[styles.page, { backgroundColor: page.color }]}
>
<Text style={styles.title}>{page.title}</Text>
</View>
))}
</PagerView>
<View style={styles.controls}>
{PAGES.map((page, index) => (
<Pressable
key={page.key}
style={styles.button}
onPress={() => pagerRef.current?.setPage(index)}
>
<Text style={styles.buttonText}>Go to {index}</Text>
</Pressable>
))}
<Pressable
style={styles.button}
onPress={() => pagerRef.current?.setPageWithoutAnimation(0)}
>
<Text style={styles.buttonText}>Jump to 0 (no animation)</Text>
</Pressable>
<Pressable
style={[styles.button, !scrollEnabled && styles.buttonDisabled]}
onPress={() => {
const next = !scrollEnabled;
setScrollEnabled(next);
pagerRef.current?.setScrollEnabled(next);
}}
>
<Text style={styles.buttonText}>
Scroll: {scrollEnabled ? 'ON' : 'OFF'}
</Text>
</Pressable>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: { flex: 1 },
pager: { flex: 1 },
page: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
title: { fontSize: 32, fontWeight: 'bold', color: '#fff' },
controls: {
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'center',
padding: 16,
gap: 8,
},
button: {
backgroundColor: '#6200ee',
paddingHorizontal: 16,
paddingVertical: 10,
borderRadius: 8,
},
buttonDisabled: { backgroundColor: '#999' },
buttonText: { color: '#fff', fontWeight: '600' },
});