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

208
Views
How to pass a value to a modal

For example, i have this functon that returns to me some ids and i get delete this posts with the id's returned with: onClick={() => { Delete(idvalue[i]) }}

However I need to open a modal before deleting the item asking if the person is sure they want to delete, and when they click on the yes I have no idea how to pass the right value to delete and where will stay this onClick to open the modal.

function Idchecker() {
    for (let i = 0; i < idvalue.length; i++) {
        if (idvalue[i] == item.id) {
            return (
                <div className="img-array">
                    <button className="click-img" onClick={() => { Delete(idvalue[i]) }}>
                        <img src={trash} alt="botão excluir" className="imgsbtns" />
                    </button>

                    <button className="click-img">
                        <img src={edit} alt="botão editar" className="imgsbtns" />
                    </button>
                </div>
            )
        }
    }
}

the modal:

return (
        <Modal
            {...props}
            size="lg"
            aria-labelledby="contained-modal-title-vcenter"
            centered
            style={{ fontFamily: "Questrial" }}
        >
            <Modal.Header>
                <Modal.Title id="contained-modal-title-vcenter">
                    DELETE THE POST
                </Modal.Title>
            </Modal.Header>
            <Modal.Body>
                <p>
                    are you sure?
                </p>
                <Button onClick={Delete}>YES</Button>
            </Modal.Body>
            <Modal.Footer>
                <Button onClick={props.onHide}>CLOSE</Button>
            </Modal.Footer>
        </Modal>
    );
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Add two state:

  • openDeleteModal to manage opening modal (argument for example, you have this argument yet)
  • deleteId to store id.

const [openDeleteModal, setOpenDeleteModal] = useState(false);
const [deleteId, setDeleteId] = useState(null);

When you click on post you must open modal and update deleteId, so rewrite onClick function:

onClick={()=>{
    setDeleteId(idvalue[i]);
    setOpenDeleteModal(true)}
    }

Pass deleteId and setOpenModal via props to Modal component and rewrite modal buttons function:

<Modal.Body>
                <p>
                    are you sure?
                </p>
                <Button 
                    onClick={()=>{
                    Delete(deleteId)
                    setOpenDeleteModal(false) //close modal after delete
                    }}>
                    YES
                 </Button>
            </Modal.Body>
            <Modal.Footer>
                <Button onClick={()=>setOpenDeleteModal(false)}>CLOSE</Button>
            </Modal.Footer>

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!