lobsterdeer
lobsterdeer

Reputation: 11

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(){
    alert(solutionStatement)
  }

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

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

Upvotes: 0

Views: 127

Answers (1)

Mvrocha
Mvrocha

Reputation: 403

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>

Upvotes: 2

Related Questions