Skip to content

Commit 2597250

Browse files
authored
docs: create a performance optimization tips documentation (#60)
1 parent c8ea44b commit 2597250

File tree

4 files changed

+186
-2
lines changed

4 files changed

+186
-2
lines changed

โ€Ždocs/docs/en/guide/_meta.jsonโ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
"type": "dir",
99
"name": "usage",
1010
"label": "Usage"
11-
}
11+
},
12+
"performance-optimization-tips"
1213
]
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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).

โ€Ždocs/docs/ko/guide/_meta.jsonโ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
"type": "dir",
99
"name": "usage",
1010
"label": "์‚ฌ์šฉ๋ฒ•"
11-
}
11+
},
12+
"performance-optimization-tips"
1213
]
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# ์„ฑ๋Šฅ ์ตœ์ ํ™” ํŒ
2+
3+
#### `renderItem` ํ•จ์ˆ˜๋Š” `useCallback`์œผ๋กœ ๊ฐ์‹ธ์„œ ๋ถˆํ•„์š”ํ•œ ๋ฆฌ๋ Œ๋”๋ง์„ ๋ฐฉ์ง€ํ•˜์„ธ์š”.
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+
#### ๋Œ€์šฉ๋Ÿ‰ ์ด๋ฏธ์ง€์˜ ๊ฒฝ์šฐ [`expo-image`](https://docs.expo.dev/versions/latest/sdk/image/)๋‚˜ [`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+
#### ๋งŽ์€ ์ˆ˜์˜ ์ด๋ฏธ์ง€๋ฅผ ๋‹ค๋ฃฐ ๋•Œ๋Š” [`FlashList`](https://shopify.github.io/flash-list/) ์‚ฌ์šฉ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.
61+
62+
- [FlashList Performance ๊ฐ€์ด๋“œ](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+
#### ๋””๋ฐ”์ด์Šค์—์„œ ํ…Œ์ŠคํŠธํ•˜์„ธ์š”. (์‹œ๋ฎฌ๋ ˆ์ดํ„ฐ์—์„œ๋Š” ์„ฑ๋Šฅ์ด ์ œํ•œ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.)

0 commit comments

Comments
ย (0)