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

230
Views
Type Script - React Native: ¿Cuál es la mejor forma de práctica para escribir ternario dentro de ternario?

¿Cuál es la mejor forma de práctica para escribir ternario dentro de ternario como en mi código de ejemplo? Quiero que el código sea más legible y correcto y quiero saber la forma correcta de escribirlo.

 <TouchableOpacity style={ darkMode ? filterState === 'A' ? styles.activeButtonDark : styles.buttonDark : filterState === 'A' ? styles.activeButton : styles.button } > </TouchableOpacity> <TouchableOpacity style={ darkMode ? filterState === 'B' ? styles.activeButtonDark : styles.buttonDark : filterState === 'B' ? styles.activeButton : styles.button } > </TouchableOpacity>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Ternario anidado dentro de ternario nunca será un buen enfoque, en cuanto a la legibilidad

 rather than this you can try with : const darkModeStyle = (filterState) => { return filterState === 'A' ? styles.activeButtonDark : styles.buttonDark } const lightModeStyle = (filterState) => { return filterState === 'A' ? styles.activeButton : styles.button } style = { darkMode ? darkModeStyle(filterState) : lightModeStyle(filterState) }

Este sigue siendo un mejor enfoque, o puede usar SwitchCase o, si no, en una función separada.

Espero eso ayude. siéntase libre de dudas

EDITAR:

Si otra cosa para ser así

 const styleGenerator = (darkMode = false, filterState = "A") => { if (darkMode) { if (filterState === 'A') { return styles.activeButtonDark } if (filterState === 'B') { return styles.stylesNew } return styles.buttonDark } else { if (filterState === 'A') { return styles.activeButton } if (filterState === 'B') { return styles.newStyle } return styles.button } } style = { styleGenerator(darkMode, filterState) }
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!