1

I have a button that looks like:

<Grid item xs={4} sm={12} md={6}>
                    <Button
                      variant="contained"
                      color="success"
                      type="submit"
                      onClick={(handleClick, handleSubmit)}
                      value="Send"
                    >
                      Send message
                    </Button>
                    <Snackbar
                      open={open}
                      autoHideDuration={5000}
                      onClose={handleClose}
                    >
                      <Alert
                        variant="filled"
                        severity="success"
                        autoHideDuration={500}
                        onClose={handleClose}
                      >
                        {"Message sent!"}
                      </Alert>
                    </Snackbar>
                  </Grid>

The button only calls the first function passed to onClick send button. How do make it so that both functions are called?

3
  • 1
    function handleClickAndHandleSubmit() { this.handleClick(); this.handleSubmit() }; Commented Dec 21, 2021 at 20:20
  • You can use something like onClick={()=> { handleSubmit(); handleClick } } Commented Dec 21, 2021 at 20:20
  • Why are people posting answers in the comments instead of as answers? One line answers can be appropriate. Commented Dec 21, 2021 at 20:21

2 Answers 2

3
const onSendMsg = () => {
  handleClick()
  handleSubmit()
}

Then change to onClick={onSendMsg}

Sign up to request clarification or add additional context in comments.

Comments

1

wrap both functions inside a parent function and execute both on the click

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.