1

created an Offcanvas component and inside it a Button having onClick event handler. the problem is whenever the component is loaded the onClick fires automatically. I don't have any idea why this happens so tried to find any instruction here. my code:

      <Offcanvas show={canvas} onHide={handleClose} scroll="true" style={{fontSize: 12}}>
        <Offcanvas.Header closeButton>
          <Offcanvas.Title>
            <Row className="py-3">
              <Col>
                <h5>Order entries</h5>
              </Col>
              <Col>
                <Button variant="danger" onClick={alert('hello')}>Reset</Button>
              </Col>
            </Row>
          </Offcanvas.Title>

        </Offcanvas.Header>
        <Offcanvas.Body>
          <Entries />
        </Offcanvas.Body>
      </Offcanvas>
0

2 Answers 2

3

Just use a function inside event attributes to prevent the problem, like this:

<Button onClick={() => alert('hello')}>Reset</Button>
Sign up to request clarification or add additional context in comments.

Comments

2

Try changing onClick={alert('hello') to onClick={() => alert('hello')

Here's a good explanation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.