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

277
Views
cómo manejar estados booleanos múltiples en un evento onClick en React;

Tengo 6 menús desplegables en la barra lateral.

cuando hago clic en el título, se mostrará el submenú.

Puedo manejar eso con cada función onClick.

me gusta

 const [state, setState] = useState({ one: false, two: false, three: false, four: false, five: false, six: false, }); const oneHandler = (e) => { e.preventDefault(); setState({ ...state, one: !state.one }); } const twoHandler = (e) => { e.preventDefault(); setState({ ...state, two: !state.two }); }

y a cambio

 <div onClick={oneHandler}>First</div> {state.one ? ( <div>Something</div> ):( "" )} <div onClick={twoHandler}>Second</div> {state.two ? ( <div>Something</div> ):( "" )} ...repeat

funciona, pero creo que los códigos son malos.

Entonces, traté de hacer eso usando un evento onClick.

me gusta

 const clickHandler = (value) => { setState({...state, value: !state.value}); }; ... <div onClick={()=> oneHandler(one)}>First</div>

pero en la consola, solo muestra falso.

Solo quiero dejarlo claro y en breve

¿Me pueden recomendar algún otro método?

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

0

const clickHandler = (value) => { setState({...state, [value]: !state[value]}); }; ... <div onClick={()=> clickHandler('one')}>First</div> <div onClick={()=> clickHandler('two')}>First</div> <div onClick={()=> clickHandler('three')}>First</div>

Supongo que esto resolverá tu problema.

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!