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

85
Views
Ejecute useEffect solo al enviar

Tengo este componente funcional -

 function ModelPredict({ model }) { const [predictionDate, setPredictionDate] = useState('') const [predictionValue, setPredictionValue] = useState('') const [isLoading, setIsLoading] = useState(false) useEffect(() => { const fetchPrediction = async () => { setIsLoading(true) const res = await fetch(`http://localhost:8000/api/predict/${model.id}/?` + new URLSearchParams({pred_date: predictionDate})) const predictionValue = await res.json() setPredictionValue(predictionValue) setIsLoading(false) } if (predictionDate) fetchPrediction() }, [predictionDate, model]) return ( <div> <label>Select Prediction date: </label> <input type="date" name="date" value={predictionDate} onInput={e => setPredictionDate(e.target.value)} /> <input type="submit" value="Submit"/> {isLoading && <p>Predicting ...</p>} {!isLoading && predictionValue && <p>Prediction: {predictionValue.prediction}</p>} </div > ) }

Actualmente, useEffect se activará cada vez que se ingrese la fecha.
Quiero que useEffect se ejecute solo cuando se hace clic en el botón Enviar.

Una forma de hacerlo sería tener otro estado inpReady y llamar a setInpReady(true) dentro de onSubmit y luego verificar dentro de useEffect si inpReady es verdadero antes de llamar a fetchPrediction .
¿Hay una mejor manera?

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

0

No sé si estoy entendiendo tu pregunta, pero ¿cuál es la necesidad de useEffect aquí? ¿Cuál es el problema de eliminar todo el código de useEffect y ponerlo todo dentro de su función onSubmit? eso solucionaria tu problema.

about 4 years ago · Juan Pablo Isaza Report

0

Una opción puede ser configurar un indicador isSubmmited y usarlo como una dependencia en useEffect:

 const [isSubmmited, setIsSubmitted] = useState(false)

Y luego onSubmit para llamar a setIsSubmmited(true)

Otra opción es simplemente dejar que onSubmit maneje la lógica que desea en useEffect

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!