Javascript alert inside a function running on its own

I have a function whose whole purpose is to run an alert which displays a message from a variable. Whenever I run the program, however, the alert runs on its own immediately, and never when I click the button that's supposed to trigger the function. I don't know why this is able to happen, so any help would be appreciated.

The Function

function finalTally(){

The Button (a small part of a larger return statement)

<div className="solutionBox">
    <button onClick={finalTally()}>Go</button>

1 answer

  • answered 2021-05-03 18:02 Mvrocha

    Probably the function is running automatticaly since you are calling it right away by using (), you could use this notation to only run when you click it:

    <button onClick={() => finalTally()}>Go</button>

    Or just pass the function without ():

    <button onClick={finalTally}>Go</button>