Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onClick function in React not working?

My simple react code is:

main.js:

var ReactDom = require('react-dom');

var Main = React.createClass({
    render: function(){
    return(
        <div>

            <a onClick={alert("hello world")} >hello</a>

    </div>

        )

    }
});

in console there were an error :

TypeError: listener must be a function

ERROR:

while running this code, I am getting that alert function instead of that this should be on click function.

Note: I am using flux structure but there were no data populating in given file.

Thanks in advance

like image 305
Ajay Kumar Avatar asked Jan 06 '23 19:01

Ajay Kumar


1 Answers

you should wrap the alert in a function you can use fat arrows to do the job

<a onClick={() => alert("hello world")} >hello</a>
like image 187
elreeda Avatar answered Jan 19 '23 12:01

elreeda