Close modal using bootstrap CDNs and react

Viewed 30

I'm using bootstrap V5 linked to my index.html with CDN links, i want to add bootstrap modal to my react project (inside the return() of my component) like this:

<div className="modal fade" id="modalEditarIntegrante" tabIndex="-1" aria-labelledby="modalEditarIntegrante" aria-hidden="true">
            <div className="modal-dialog">
                <div className="modal-content">
                <div className="modal-header">
                    <h5 className="modal-title">Editar Integrante</h5>
                    <button type="button" className="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div className="modal-body">
                    <form onSubmit={editarIntegrante}>
                        <div className="row mb-3 justify-content-between">
                            <div className="col-md-5">
                                <label htmlFor="nombreIntegranteEditar">Nombre</label>
                                <input type="text" id="nombreEditarIntegrante" className="form-control" 
                                value={nombreIntegranteEditar} onChange={(e)=>setNombreIntegranteEditar(e.target.value)}/>
                            </div>
                            <div className="col-md-6">
                                <label htmlFor="apellidoIntegranteEditar">Apellido</label>
                                <input type="text" id="apellidoEditarIntegrante" className="form-control" 
                                value={apellidoIntegranteEditar} onChange={(e)=>setApellidoIntegranteEditar(e.target.value)}/>
                            </div>
                        </div>
                        <div className="row mb-3 justify-content-between">
                            <div className="col-md-7">
                                <label htmlFor="nickIntegranteEditar">Nick</label>
                                <input type="text" id="nickEditarIntegrante" className="form-control"
                                 value={nickIntegranteEditar} onChange={(e)=>setNickIntegranteEditar(e.target.value)}/>
                            </div>
                            <div className="col-md-4">
                                <label htmlFor="edadIntegranteEditar">Edad</label>
                                <input type="number" id="edadEditarIntegrante" className="form-control"
                                 value={edadIntegranteEditar} onChange={(e)=>setEdadIntegranteEditar(e.target.value)}/>
                            </div>
                        </div>
                        <label htmlFor="juegoFavIntegranteEditar">Juego Fav</label>
                        <input type="text" id="juegoFavIntegranteEditar" className="form-control w-" 
                        value={juegoFavIntegranteEditar} onChange={(e)=>setJuegoFavIntegranteEditar(e.target.value)}/>
                        
                        <div className="modal-footer">
                            <button type="button" className="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                            <button type="submit" className="btn btn-primary">Save changes</button>
                        </div>
                    </form>
                    
                </div>
                    
                </div>
                
            </div>
        </div>

And after submit event, i want to be able to close the modal with id= 'modalEditarIntegrante'

So after the function submited "editarIntegrante" i want to close the modal

const editarIntegrante = async (e)=>{
    e.preventDefault();
    await fetch(`${URI}editarIntegrante`,{
        method:'POST',
        headers:{
            'content-type':'application/json'
        },
        body: JSON.stringify({
            'idIntegranteEditar': idIntegranteEditar,
            'nombreIntegrante': nombreIntegranteEditar,
            'apellidoIntegrante':apellidoIntegranteEditar,
            'nickIntegrante':nickIntegranteEditar,
            'desIntegrante':desIntegranteEditar,
            'edadIntegrante': edadIntegranteEditar,
            'juegoFavIntegrante': juegoFavIntegranteEditar,
            'imgIntegrante':imgIntegranteEditar
        })
    }).then(res=>res.json()).then(data=>{
        obtenerIntegrantes();
        let modal = document.getElementById('modalEditarIntegrante') 
        modal.hide();
        
    })
}
1 Answers

Regarding Bootstrap 5 documentation You must initialize the modal in order to control it.

var ModaleditarIntegrante = new bootstrap.Modal('#modalEditarIntegrante');

After that you can use .hide()

ModaleditarIntegrante.hide();

A full example could be this one, which is going to hide the popup every 2 seconds:

var myModal = new bootstrap.Modal('#exampleModal');

// Toggle every 1 seconds
setInterval( function() {  
  // Here is is toggle for the example but you can use .show() or .hide() depending your requirements
  myModal.toggle();
 },1000);
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

<!-- Modal -->
<div class="modal fade" id="exampleModal">
  <div class="modal-dialog">
    <div class="modal-content">
      Modal Content
    </div>
  </div>
</div>

Related