React's onClick function not working?

My simple 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>

        )

    }
});

An error occurred in the console:

TypeError: the listener must be a function

Error:

while running this code, I get this warning function instead of what it should be on the click function.

Note . I am using a stream structure, but there was no data in the given file.

Thanks in advance

+4
source share
2 answers

you must wrap the alert in a function that you can use with thick arrows to complete the task

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

onClick , , , .

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

var Main = React.createClass({
    render: function(){
        return (
            <div>
                 <a onClick={function () {alert("hello world")}} >hello</a>
            </div>
        )
    }
});
+4

All Articles