Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React: Button onClick function is running on page load but not you click it

I am using the ButtonGroup and Button from ReactStrap. I have set an onClick function when you click one of the buttons:

< ButtonGroup >
  <Button>Edit</Button>
  <Button onClick={console.log("Print some text")}>Print text</Button>
  <Button>Set as default</Button>
</ButtonGroup >

But when I load the page this is what I get: enter image description here

Before I have even clicked the button. And if I do click it, nothing comes out in the console. Any ideas?

like image 596
Slaknation Avatar asked Jan 11 '19 17:01

Slaknation


1 Answers

Onclick must be function. You just set onlick as result of console.log("Print some text")

Try this

   <Button onClick={() => {console.log("Print some text")}}>Print text</Button>
like image 99
aseferov Avatar answered Sep 19 '22 22:09

aseferov