2015-03-30 1 views
8

У меня есть компонент, который получает список записей в качестве опоры. Я пытаюсь добавить функцию к событию onClick, но код, который будет запущен в событии click, запускается, как только компонент загружается. В этом случае я имею в виду функцию processM. Я новичок в ReactJS, и я был бы признателен за любую помощь.Функция, указанная для события onClick, запускается во время загрузки на ReactJS

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

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

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

 
\t }, 
 
\t render: function(){ 
 

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

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

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

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

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

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

ответ

15

this.processM(upUrl) будет вызывать функцию. Чтобы отправить переменные, которые вы должны связать, как это вместо

this.processM.bind (это, upUrl)

Here is a good example with bind

+1

Dhiraj, это сработало красиво. Спасибо. – chesco

+0

, или вы можете просто сделать onclick {this.processM} и установить атрибут data. Итак, data-upurl = {upUrl} и внутри функции захватывают его с помощью event.target.dataset.upurl – zackify

Смежные вопросы