Skip to content

Commit 856c2ce

Browse files
committed
refactor example to avoid render collisions
upgrade example to react-native 0.79.2
1 parent 8ed299c commit 856c2ce

File tree

3 files changed

+591
-1980
lines changed

3 files changed

+591
-1980
lines changed

example/App.tsx

Lines changed: 56 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ import React from 'react';
22
import {
33
StyleSheet,
44
Text,
5-
View,
65
FlatList,
7-
SafeAreaView,
86
TouchableOpacity,
9-
ImageSourcePropType,
107
ColorValue,
8+
SafeAreaView,
119
} from 'react-native';
1210
import DropdownAlert, {
1311
DropdownAlertData,
@@ -18,36 +16,14 @@ import DropdownAlert, {
1816
import NotificationIOS from './NotificationIOS';
1917
import NotificationAndroid from './NotificationAndroid';
2018

21-
type ListItem = {
19+
interface ListItem {
2220
name: string;
2321
alertData?: DropdownAlertData;
2422
alertProps?: DropdownAlertProps;
2523
color: ColorValue;
26-
};
27-
28-
type ListItemIndex = {
29-
item: ListItem;
30-
index: number;
31-
};
32-
33-
function App(): React.JSX.Element {
34-
const defaultSelected: ListItem = {
35-
name: 'Default',
36-
color: DropdownAlertColor.Default,
37-
};
38-
const [selected, setSelected] = React.useState(defaultSelected);
39-
let alert = React.useRef(
40-
(_data?: DropdownAlertData) => new Promise<DropdownAlertData>(res => res),
41-
);
42-
let dismiss = React.useRef(() => {});
43-
const reactNativeLogoSrc: ImageSourcePropType = {
44-
uri: 'https://reactnative.dev/img/pwa/manifest-icon-512.png',
45-
};
46-
47-
React.useEffect(() => {
48-
alert.current(selected.alertData);
49-
}, [selected]);
24+
}
5025

26+
export default function App(): React.JSX.Element {
5127
const items: ListItem[] = [
5228
{
5329
name: 'Warn',
@@ -95,13 +71,18 @@ function App(): React.JSX.Element {
9571
title: 'Custom',
9672
message:
9773
'This demonstrates the ability to customize image, interval and style.',
98-
source: reactNativeLogoSrc,
74+
source: {
75+
uri: 'https://reactnative.dev/img/pwa/manifest-icon-512.png',
76+
},
9977
interval: 5000,
10078
},
10179
alertProps: {
102-
alertViewStyle: styles.alertView,
80+
alertViewStyle: {
81+
padding: 8,
82+
backgroundColor: '#6441A4',
83+
},
10384
},
104-
color: styles.alertView.backgroundColor,
85+
color: '#6441A4',
10586
},
10687
{
10788
name: 'iOS notification',
@@ -149,64 +130,78 @@ function App(): React.JSX.Element {
149130
color: 'green',
150131
},
151132
];
133+
const [selected, setSelected] = React.useState<ListItem | undefined>();
134+
let alert = React.useRef(
135+
(_data?: DropdownAlertData) => new Promise<DropdownAlertData>(res => res),
136+
);
137+
let dismiss = React.useRef(() => {});
152138

153-
function _renderItem(listItemIndex: ListItemIndex): React.JSX.Element {
154-
const {item} = listItemIndex;
139+
React.useEffect(() => {
140+
if (selected) {
141+
if (
142+
selected.alertProps?.alertPosition ||
143+
selected.alertProps?.children ||
144+
selected.alertProps?.showCancel
145+
) {
146+
dismiss.current();
147+
}
148+
alert.current(selected.alertData);
149+
}
150+
}, [selected]);
151+
152+
function _renderItem(item: ListItem): React.JSX.Element {
155153
return (
156154
<TouchableOpacity
157155
style={[styles.item, {backgroundColor: item.color}]}
158-
onPress={() => _onSelect(item)}>
156+
onPress={() => setSelected(item)}>
159157
<Text style={styles.name}>{item.name}</Text>
160158
</TouchableOpacity>
161159
);
162160
}
163161

164-
function _onSelect(item: ListItem): void {
165-
setSelected(item);
162+
function _renderFooter(): React.JSX.Element {
163+
return (
164+
<TouchableOpacity style={styles.item} onPress={dismiss.current}>
165+
<Text style={styles.name}>{'Dismiss'}</Text>
166+
</TouchableOpacity>
167+
);
166168
}
167169

168170
return (
169-
<View style={styles.view}>
170-
<SafeAreaView>
171-
<FlatList
172-
keyExtractor={(_item, index) => `${index}`}
173-
data={items}
174-
initialNumToRender={items.length}
175-
renderItem={_renderItem}
176-
/>
177-
</SafeAreaView>
178-
<DropdownAlert
179-
alert={func => (alert.current = func)}
180-
dismiss={func => (dismiss.current = func)}
181-
{...selected.alertProps}
171+
<SafeAreaView style={styles.view}>
172+
<FlatList
173+
keyExtractor={(_item, index) => `${index}`}
174+
data={items}
175+
initialNumToRender={items.length}
176+
renderItem={({item}) => _renderItem(item)}
177+
ListFooterComponent={_renderFooter}
182178
/>
183-
</View>
179+
{selected && (
180+
<DropdownAlert
181+
alert={func => (alert.current = func)}
182+
dismiss={func => (dismiss.current = func)}
183+
{...selected.alertProps}
184+
/>
185+
)}
186+
</SafeAreaView>
184187
);
185188
}
186189

187190
const styles = StyleSheet.create({
188191
view: {
189192
flex: 1,
190-
justifyContent: 'center',
191193
backgroundColor: '#F4F3E9',
192194
},
193195
item: {
194-
padding: 12,
195-
margin: 4,
196+
padding: 8,
197+
margin: 8,
196198
borderRadius: 8,
197-
borderColor: 'black',
198-
borderWidth: StyleSheet.hairlineWidth,
199+
backgroundColor: 'black',
199200
},
200201
name: {
201202
fontSize: 18,
202203
fontWeight: 'bold',
203204
textAlign: 'center',
204205
color: 'whitesmoke',
205206
},
206-
alertView: {
207-
padding: 8,
208-
backgroundColor: '#6441A4',
209-
},
210207
});
211-
212-
export default App;

example/package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22
"name": "dropdownalertexample",
33
"version": "1.0.0",
44
"main": "index.ts",
5+
"private": true,
56
"scripts": {
67
"start": "expo start",
78
"android": "expo start --android",
89
"ios": "expo start --ios",
910
"web": "expo start --web"
1011
},
1112
"dependencies": {
12-
"expo": "~52.0.17",
13-
"react": "18.3.1",
14-
"react-native": "0.76.3",
13+
"expo": "~53.0.9",
14+
"react": "19.0.0",
15+
"react-native": "0.79.2",
1516
"react-native-dropdownalert": "5.1.0"
1617
},
1718
"devDependencies": {
1819
"@babel/core": "^7.25.2",
19-
"@types/react": "~18.3.12",
20-
"typescript": "^5.3.3"
20+
"@types/react": "~19.0.10",
21+
"typescript": "^5.8.3"
2122
},
22-
"private": true,
23-
"packageManager": "[email protected]"
23+
"packageManager": "[email protected]"
2424
}

0 commit comments

Comments
 (0)