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 }
);
}
希望这会有所帮助!
关于javascript - 如何在React Native中使用Alert.alert中的foreach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52139399/