gpt4 book ai didi

reactjs - 为 onClick 事件列出的函数在 ReactJS 上加载时触发

转载 作者:行者123 更新时间:2023-12-03 13:23:09 25 4
gpt4 key购买 nike

我有一个组件正在接收记录列表作为 Prop 。我正在尝试向 onClick 事件添加一个函数,但是一旦组件加载,要在 click 事件上执行的代码集就会触发。在本例中,我指的是 processM 函数。我是 ReactJS 的新手,希望得到任何帮助。

var TempJobTaskActions = React.createClass({
processM: function(url){

console.log('in processMove: ', url);

$.ajax({
type: 'GET',
url: url,
success: function(data){
}
});

},
render: function(){

var action = null;
var downUrl = '../MoveDown?id='+this.props.tid;
var upUrl = '../MoveUp?id='+this.props.tid;

if(this.props.position == "solo"){
action = (<span></span>);
}

if(this.props.position == "first"){
action = (<span><a className='cdc-link cdc-icon glyphicon glyphicon-arrow-down'
href='#' onClick={this.processM(downUrl)} title='Move Down'></a></span>);
}

if(this.props.position == "last"){
action = (<span><a className='cdc-link cdc-icon glyphicon glyphicon-arrow-up'
href='#' onClick={this.processM(upUrl)} title='Move Up'></a></span>);
}

if(this.props.position == "middle"){
action = (
<div>
<span><a className='cdc-link cdc-icon glyphicon glyphicon-arrow-up'
href='#' onClick={this.processM(upUrl)} title='Move Up'></a></span>
<span><a className='cdc-link cdc-icon glyphicon glyphicon-arrow-down'
href='#' onClick={this.processM(downUrl)} title='Move Down'></a></span>
</div>
);
}

return (
<div>{action}</div>
);
}
});

最佳答案

this.processM(upUrl) 将调用该函数。要发送变量,您应该像这样绑定(bind)

this.processM.bind(this, upUrl)

Here is a good example with bind

关于reactjs - 为 onClick 事件列出的函数在 ReactJS 上加载时触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29351118/

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