gpt4 book ai didi

javascript - React 中的 onClick 函数不起作用?

转载 作者:搜寻专家 更新时间:2023-11-01 05:04:03 25 4
gpt4 key购买 nike

我的简单 react 代码是:

主要.js:

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

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

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

</div>

)

}
});

控制台出现错误:

TypeError: listener must be a function

错误:

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

注意:我正在使用 flux 结构,但给定文件中没有数据填充。

提前致谢

最佳答案

您应该将警报包装在一个函数中,您可以使用粗箭头来完成这项工作

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

关于javascript - React 中的 onClick 函数不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38146517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com