反应 onClick 事件 [英] React onClick event
本文介绍了反应 onClick 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遗漏了一些东西.这是一个非常简单的 hello world,目标是触发 onClick 警报事件.该事件会在页面加载时触发,但在我单击按钮时不会触发.我很感激你的帮助.这是一个 jsFiddle,可以更容易地查看:jsFiddle
I'm missing something. Here's a very simple hello world, the goal is to just fire an alert event onClick. The event does fire when the page loads, but not when I click the button. I appreciate the help. Here's a jsFiddle to make it easier to see: jsFiddle
var Hello = React.createClass({
render: function() {
return <button onClick={alert("Hello World!")}>
Click Me
</button>;
}
React.render(<Hello />, document.getElementById('container'));
推荐答案
我认为您的做法是错误的,因为 ReactJS 只是 JavaScript 我认为您触发此事件的方式行不通.你的 onClick 应该像这样触发一个附加到你的 React 元素的函数.
I think you're going about this wrong, because ReactJS is just JavaScript I don't think your way of firing this event will work. Your onClick should fire a function attached to your React element like so.
var Hello = React.createClass({
myClick: function () {
alert("Hello World!");
},
render: function() {
return <button onClick={this.myClick}>
Click Me
</button>;
}
});
React.render(<Hello />, document.getElementById('container'));
这篇关于反应 onClick 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文