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

183
Views
Encontrar un objeto específico en el estado, eliminarlo y reemplazarlo en React

Mi código actual encuentra un objeto en una matriz de state , lo elimina y luego agrega un newValue en su lugar. Sé que usar .splice() muta el estado directamente, lo cual no es una práctica recomendada en React. ¿Cuál es la forma correcta de hacer lo que estoy tratando de hacer a continuación?

 const found = clientServiceReferral.selectedTimeSlots.find(element => element.timeSlot === timeWithDate); if (found) { clientServiceReferral.selectedTimeSlots.splice(found, 1) } const newValue = { timeSlot: timeWithDate, value: value.value } setClientServiceReferral({ ...clientServiceReferral, selectedTimeSlots: [...clientServiceReferral.selectedTimeSlots, newValue] })
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Usaría el método de mapa, que devuelve una nueva matriz, para buscar y reemplazar:

 const updatedTimeSlots = clientServiceReferral.selectedTimeSlots.map((element) => { if(element.timeSlot === timeWithData){ return {timeSlot: timeWithDate, value: value.value} } else { return element } }) setClientServiceReferral({ ...clientServiceReferral, selectedTimeSlots: updatedTimeSlots })
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!