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

169
Views
Cómo establecer un atributo predeterminado cuando se establece un atributo múltiple en un solo estado en useState en reaccionar
 const [currentTime, setTime] = useState(); const [input, setInput] = useState({ title:"", description:"", time:currentTime, }); function handleOnclickAdd(){ setTime(new Date().toLocaleTimeString()) }

aquí, cuando hago console.log(currentTime) muestra la salida perfecta, pero cuando hago console.log(input) en ese momento, muestra el tiempo de salida: indefinido. luego Cómo configurar la hora como la hora actual.

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

0

cuando el estado de input se está initializing , el currentTime no está undefined , por lo que si desea cambiarlo a otro valor, simplemente cambie el valor a lo que quiera de esta manera:

 const [currentTime, setTime] = useState(); const [input, setInput] = useState({ title: "", description: "", time: new Date().toLocaleTimeString() }); function handleOnclickAdd() { setTime(new Date().toLocaleTimeString()); }
about 4 years ago · Juan Pablo Isaza Report

0

porque en la primera etapa, currentTime es nulo, por lo que debe usar la función useEffect para manejar esto:

 const [currentTime, setTime] = useState(); const [input, setInput] = useState({ title:"", description:"", time:currentTime, }); function handleOnclickAdd(){ setTime(new Date().toLocaleTimeString()) } useEffect(() => { setInput(...input , [time] : currentTime); },[currentTime])
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!