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

99
Views
El estado de configuración no funciona después de la primera llamada a la API en reaccionar

Estoy enviando una solicitud POST a un punto final para reaccionar usando axios y si mi respuesta devuelta es exitosa, estoy configurando algún estado.

Aquí está mi código:

 axios.post('/some/endpoint', {mydata}) .then(res => { console.log(res.data); if(res.data.success) { setMsg('successful'); setActive('alert alert-success fade-out'); } else { setMsg('Oops! An error occured!'); setActive('alert alert-danger'); } })

Después de la primera solicitud de API, todo está bien, recibo el mensaje y se desvanece, aunque si intento enviar otra solicitud, no vuelve a aparecer y comienza a desvanecerse, ¿por qué no se activan las llamadas setMsg y setActive? ?

Aquí está el render:

 return ( <> <Modal open={open} onClose={onCloseModal} center closeIcon={closeIcon} modalId="response-modal"> <div className="qr-modal-header-stock"> <h5>Enter fulfillment stock</h5> <p>{title}</p> <p>Fulfilment Center: {fulfilmentCenterName}</p> <p>Existing stock: {stock}</p> <p className={active} style={{opacity:0}}>{msg}</p> <-- this appears firstly but not the second time when I submit the form, why? <form onSubmit={handleSubmit}> <input type="hidden" name="ean" value={data} /> <input type="hidden" name="product_stock" value={newStock} /> <input type="number" class="form-control" onChange={e => setNewStock(parseInt(e.target.value) + parseInt(stock))}/> <br /> <input type="submit" class="btn btn-primary form-control" id="submit-fulfilment-center" value="Save"/> </form> <br /> <p>New stock: {isNaN(newStock) ? 0 : newStock}</p> <button className="btn btn-primary" onClick={onCloseModal}>Scan another ean</button> </div> </Modal> </ > );
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Debe usar el enlace "useState" y observar los estados "Msg" y "Active" para ver si hay cambios.

 useState(()=>{ // do somthing here },[Msg,Active])

usar esto volverá a mostrar la página cada vez que cambie el valor de "Msg" o "Active".

Aviso: no establezca estados y mírelos en el mismo enlace useState, esto provocará un bucle de reproducción infinito

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!