Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

245
Views
Alerta React-Native: configure dinámicamente dos o tres botones en la alerta

Necesito configurar una alerta en React-Native (también estoy usando Expo, si eso importa aquí) pero me gustaría configurarlo para que muestre 2 o 3 botones según los datos que le envíe. Un ejemplo sencillo sería:

 import { Alert } from 'react-native'; const showConfirmationAlert = (val) => { return Alert.alert( "Alert title", "Alert text", [ val !== null ? { text: "Option A", onPress: () => { ... } } : {}, { text: "Option B", onPress: () => { ... } }, { text: "Cancel"} ] ); };

En este momento, esto funciona, pero si val === nulo, aún se muestran 3 opciones, excepto que la opción A está vacía. No puedo establecer la opción A otra condición en nulo, es decir, val !== null ? { text: "Option A", onPress: () => { ... } } : null ya que arroja un error de que los objetos no pueden ser nulos.

Ciertamente puedo ejecutar la verificación val === null antes de llamar a la Alerta y simplemente crear dos Alertas, una para cada caso, pero me gustaría saber si es posible configurar este sistema y tenerlo todo dentro de una sola Alerta. .

También puedo hacer lo siguiente:

 import { Alert } from 'react-native'; const showConfirmationAlert = (val) => { return Alert.alert( "Alert title", "Alert text", val !== null ? [ { text: "Option A", onPress: () => { ... } } : {}, { text: "Option B", onPress: () => { ... } }, { text: "Cancel"} ] : [ { text: "Option B", onPress: () => { ... } }, { text: "Cancel"} ] ); };

Pero parece que hay demasiada repetición de código.

¿Hay alguna manera de lograr esto sin tener dos alertas completamente diferentes o repetir las dos opciones que siempre se mostrarán?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

¿Qué te parece esta solución?

 import { Alert } from 'react-native'; const showConfirmationAlert = (val) => { const options = [{ { text: "Option B", onPress: () => { ... } }, { text: "Cancel"} }]; if (val !== null) { options.unshift({ text: "Option A", onPress: () => { ... } }); } return Alert.alert( "Alert title", "Alert text", options, ); };

Incluso podría cambiar el nombre de las opciones a algo como "baseOptions" y crear un método que maneje los casos de esquina. En este momento, solo tiene un caso de esquina relacionado con cuando val es nulo, pero podría tener más en el futuro.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!