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

154
Views
Guardando el estado del botón onClick

Tengo:

 <blink> const [thisButtomSelected, setThisButtomSelected] = useState(false); var thisButton = []; const onAttributeClick = (e) => { thisButton[e.currentTarget.value] = { thisID: e.currentTarget.id, thisName: e.currentTarget.name } setThisButtomSelected(thisButton[e.currentTarget.value]); } return( <div> {data.item.attributes.map((attribute, index) => ( <div key={index} > <p id={attribute.id}>{attribute.name}:</p> <ul className="choose-attribute-container-ul"> {attribute.items.map((item) => ( <li key={item.id}> <button value={item.value} id={item.id} name={attribute.name} className={_.isEqual(thisButtomSelected, { thisID: item.id, thisName: attribute.name }) ? 'attribute-button-selected' : 'attribute-button'} onClick={onAttributeClick} > {item.displayValue} </button> </li> ))} </ul> </div> ))} </div> ) </blink>

Este patrón funciona bien, pero cada vez que en la página hay más de 1 atributo y el usuario selecciona más de uno, el botón previamente seleccionado no se hace clic.
Mi pregunta es: ¿Cómo puedo guardar el estado del primer botón seleccionado después de hacer clic en el segundo?

  1. para cada atributo solo un botón puede estar activo
  2. se debe usar el nombre de los botones
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Debe guardar los botones en una matriz para conservarlos todos, algo así:

 const [thisButtomSelected, setThisButtomSelected] = useState([]); var thisButton = []; const onAttributeClick = (e) => { thisButton[e.currentTarget.value] = { thisID: e.currentTarget.id, thisName: e.currentTarget.name } setThisButtomSelected([...thisButtomSelected, thisButton[e.currentTarget.value]]); } return( <div> {data.product.attributes.map((attribute, index) => ( <div key={index} > <p id={attribute.id}>{attribute.name}:</p> <ul className="choose-attribute-container-ul"> {attribute.items.map((item) => ( <li key={item.id}> <button value={item.value} id={item.id} name={attribute.name} className={thisButtomSelected.find(el => el.thisID === item.id && el.thisName === attribute.name) ? 'attribute-button-selected' : 'attribute-button'} onClick={onAttributeClick} >
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!