简体   繁体   English

React 不会转到手动构建的 JavaScript 函数

[英]React not going to JavaScript function that is manually built

I have a scenario in which I need to modify code from a promise to wrap a link around it.我有一个场景,我需要修改承诺中的代码以在它周围包装一个链接。

I came up with this.我想出了这个。

const modifiedValue = <a onClick={this.jsfunction(item.value)} href="javascript:void(0);">{item.value}</a>;

It is not working:它不起作用:

//function
jsfunction = (test) => {
   console.log('test', test)
}

I tried to bind in the constructor too:我也尝试在构造函数中绑定:

this.jsfunction = this.jsfunction.bind(this);

Seems if I remove the onClick then it is worse, also removing the href, JavaScript: is that needed ?似乎如果我删除onClick那么情况更糟,还删除了 href,JavaScript:需要吗?

The onClick handler should be the function to execute when clicked. onClick 处理程序应该是单击时要执行的函数。 Right now, you're setting the onClick whatever jsfunction resolves to.现在,您正在设置 onClick jsfunction 解析为的任何内容。 To make it work, change the onClick to: e => this.jsfunction(item.value)要使其工作,请将 onClick 更改为: e => this.jsfunction(item.value)

Use:用:

const modifiedValue = <a onClick={e=>this.jsfunction(item.value)} href="javascript:void(0);">{item.value}</a>;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM