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

180
Views
React & clsx: agregue un nombre de clase si el elemento actual en una matriz asignada es el primero de varios elementos

Tengo el siguiente código y necesito agregarle algunas funciones nuevas. Básicamente, la función de mapa itera a través de una matriz, y si solo hay un elemento en la matriz, entonces no quiero agregar una nueva clase, pero si este es el primer elemento en una matriz de 2 elementos, quiero para agregar un nombre de clase.

 {section?.values?.link.map((link, index) => { return ( <LinkComponent key={index} className={clsx({ "jc-left": link?.values?.linkType !== "primary-button", })} </LinkComponent> ...

Sé que se parece a la que ya está allí, en el sentido de que puse el nombre de la clase entre comillas seguido de un punto y coma y luego la regla, simplemente no sé cómo escribir lo que me parece una regla compleja. Cualquier ayuda es apreciada.

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

0

Si entiendo correctamente, su pregunta es que desea agregar className si array.length > 1 y luego agregar class al primer elemento de la matriz.

 {section?.values?.link.map((link, index, self) => { return ( <LinkComponent key={index} className={clsx({ "jc-left": link?.values?.linkType !== "primary-button", "YOUR_CLASS": self.length > 1 && index === 0, })} </LinkComponent>

Pero, ¿qué sucede si tiene más de dos elementos? Entonces supongo que agregará clase a todos los elementos excepto al último.

 {section?.values?.link.map((link, index, self) => { return ( <LinkComponent key={index} className={clsx({ "jc-left": link?.values?.linkType !== "primary-button", "YOUR_CLASS": self.length > 1 && (index + 1 !== self.length), })} </LinkComponent>
about 4 years ago · Juan Pablo Isaza Report

0

Si desea renderizar condicionalmente con clsx, puede hacerlo en función de las dos condiciones:

 {section?.values?.link.map((link, index) => { // Checks if array has more than 2 or 2 items and if the index is 0 which means that is the first item. const hasConditionalClass = section?.values?.link.length >= 2 && index === 0; return ( <LinkComponent key={index} className={clsx({ "jc-left": link?.values?.linkType !== "primary-button", "condtional-class": hasConditionalClass })} </LinkComponent> ...
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!