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

117
Views
Establecer estilos de clase para elementos en un componente de reacción

Traté de poner un subrayado en mi lista de navegación con este código.

 const [classOn, setClassOn] = useState('menu_selected'); const [linkMenu, setLinkMenu] = useState(''); function handleSelectedMenu(nb){ setLinkMenu(document.querySelectorAll('nav ul li')); for(let i=1; i<linkMenu.length; i++){ linkMenu[i].firstChild.classList.remove(classOn) } linkMenu[nb].firstChild.classList.add(classOn) }

en pantalla completa, no pasa nada y aparece un error en el registro de mi consola

 Uncaught TypeError: Cannot read properties of undefined (reading 'firstChild')

pero cuando abro el visor móvil, se aplican los estilos.

ingrese la descripción de la imagen aquí

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

0

Puede cambiar el nombre de la clase directamente en el elemento en función de un estado existente, como:

 function classNames(...classes) { return classes.filter(Boolean).join(" "); } function FakeElement = () => { const [hidden, setHidden] = useState(false); return ( <li className={classNames( hidden ? "hidden-class" : "", "default-class other-default-class" )} >Stuff</li> ); }

Puede agregar muchas condiciones en la función classNames.

about 4 years ago · Juan Pablo Isaza Report

0

deberías probar algo como esto.

 <ul onMouseOver={() => setLinkMenu(true)} onMouseLeave={() => setLinkMenu(false)} className={`${linkMenu ? 'block ' : 'hidden '}`} > {options.map((option) => ( <li className="text-sm p-2 block w-full" key={option} onClick={() => setLinkMenu(false)}>{option}</li> </ul>
about 4 years ago · Juan Pablo Isaza Report

0

Resuelvo el problema usando styles.menu_selected .

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!