|
| 1 | +# Performance Optimization Tips |
| 2 | + |
| 3 | +#### Wrap the `renderItem` function with `useCallback` to prevent unnecessary re-renders. |
| 4 | + |
| 5 | +```tsx |
| 6 | +import { useCallback, useState } from 'react'; // [!code highlight] |
| 7 | +import { ScrollView, Image, Modal } from 'react-native'; |
| 8 | +import { GestureViewer } from 'react-native-gesture-image-viewer'; |
| 9 | + |
| 10 | +function App() { |
| 11 | + const images = [...]; |
| 12 | + const [visible, setVisible] = useState(false); |
| 13 | + |
| 14 | + // [!code highlight:3] |
| 15 | + const renderImage = useCallback((imageUrl: string) => { |
| 16 | + return <Image source={{ uri: imageUrl }} style={{ width: '100%', height: '100%' }} resizeMode="contain" />; |
| 17 | + }, []); |
| 18 | + |
| 19 | + return ( |
| 20 | + <Modal visible={visible} onRequestClose={() => setVisible(false)}> |
| 21 | + <GestureViewer |
| 22 | + data={images} |
| 23 | + renderItem={renderImage} |
| 24 | + ListComponent={ScrollView} |
| 25 | + onDismiss={() => setVisible(false)} |
| 26 | + /> |
| 27 | + </Modal> |
| 28 | + ); |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +#### For large images, we recommend using [`expo-image`](https://docs.expo.dev/versions/latest/sdk/image/) or [`FastImage`](https://github.com/DylanVann/react-native-fast-image). |
| 33 | + |
| 34 | +```tsx |
| 35 | +import { ScrollView, Modal } from 'react-native'; |
| 36 | +import { GestureViewer } from 'react-native-gesture-image-viewer'; |
| 37 | +import { Image } from 'expo-image'; // [!code highlight] |
| 38 | + |
| 39 | +function App() { |
| 40 | + const images = [...]; |
| 41 | + const [visible, setVisible] = useState(false); |
| 42 | + |
| 43 | + const renderImage = useCallback((imageUrl: string) => { |
| 44 | + return <Image source={{ uri: imageUrl }} style={{ width: '100%', height: '100%' }} contentFit="contain" />; // [!code highlight] |
| 45 | + }, []); |
| 46 | + |
| 47 | + return ( |
| 48 | + <Modal visible={visible} onRequestClose={() => setVisible(false)}> |
| 49 | + <GestureViewer |
| 50 | + data={images} |
| 51 | + renderItem={renderImage} |
| 52 | + ListComponent={ScrollView} |
| 53 | + onDismiss={() => setVisible(false)} |
| 54 | + /> |
| 55 | + </Modal> |
| 56 | + ); |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +#### For handling many images, we recommend using [`FlashList`](https://shopify.github.io/flash-list/). |
| 61 | + |
| 62 | +- [FlashList Performance Guide](https://shopify.github.io/flash-list/docs/fundamentals/performance) |
| 63 | + |
| 64 | +```tsx |
| 65 | +import { useCallback, useState } from 'react'; |
| 66 | +import { Image, Modal } from 'react-native'; |
| 67 | +import { GestureViewer } from 'react-native-gesture-image-viewer'; |
| 68 | +import { FlashList } from '@shopify/flash-list'; // [!code highlight] |
| 69 | + |
| 70 | +function App() { |
| 71 | + const images = [...]; |
| 72 | + const [visible, setVisible] = useState(false); |
| 73 | + |
| 74 | + const renderImage = useCallback((imageUrl: string) => { |
| 75 | + return <Image source={{ uri: imageUrl }} style={{ width: '100%', height: '100%' }} resizeMode="contain" />; |
| 76 | + }, []); |
| 77 | + |
| 78 | + return ( |
| 79 | + <Modal visible={visible} onRequestClose={() => setVisible(false)}> |
| 80 | + <GestureViewer |
| 81 | + data={images} |
| 82 | + renderItem={renderImage} |
| 83 | + ListComponent={FlashList} // [!code highlight] |
| 84 | + onDismiss={() => setVisible(false)} |
| 85 | + /> |
| 86 | + </Modal> |
| 87 | + ); |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +#### Test on actual devices (performance may be limited in simulators). |
0 commit comments