[英]How to use foreach inside Alert.alert in react native
function openmodal(modalarray) {
return Alert.alert(
"Alert Title",
"My Alert Msg",
[
modalarray.forEach(element => {
"{text: element, onPress: () => console.log('button press)},";
})
],
{ cancelable: false }
);
}
請使用.map
函數而不是.forEach
。 .forEach
不返回任何值,因此您只是在數組中傳遞undefined
。
考慮以下示例
function openmodal(modalarray) {
return Alert.alert(
"Alert Title",
"My Alert Msg",
[
...modalarray.map(element =>
({text: element, onPress: () => console.log('button press')}))
],
{ cancelable: false }
);
}
希望這會有所幫助!
嘗試:
function openmodal(modalarray) {
return Alert.alert(
"Alert Title",
"My Alert Msg",
modalarray.map(element => ({
text: element,
onPress: () => console.log('button press')
})),
{ cancelable: false }
);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.