2017-02-11 55 views
-2

我需要从项目的特定信息,例如像id onClicking名单,我得到的,但我现在面临就是,如果我通过(item.Id)作为参数handleClick,它会显示项目中的所有Id。这很明显。需要显示特定的ID onClicking特定列表

那么,如何改变我的代码,以获得特定的ID在点击特定列表?

handleClick(event) { 
    console.log(event); 
     alert(event); 
    } 


     <ul className="w3-ul w3-card-4 w3-yellow"> {this.state.post.map((item, index)=> { 
          return (
           <Link to="/displaylist" style={{textDecoration:'none'}} key={index} > 
            <li className=" w3-hover-green w3-padding-16" onClick={this.handleClick}> 
             <img src={require('./3.jpg')} className="w3-left w3-circle w3-margin-right " width="60px" height="auto" /> 
              <span>{item.Firstname}</span><br/><br/> 
            </li> 
           </Link> 

           )} 
          )} 
      </ul> 
+1

这是过去24小时中的5ᵗʰ问题。 –

+0

是的,我仍然处于学习阶段,所以提问并不是错误的。 –

回答

1

你可以做什么用:

handleClick(id) { 
    return function(event) { 
     console.log(id); 
    }; 
} 


<ul className="w3-ul w3-card-4 w3-yellow"> {this.state.post.map((item, index)=> { 
    return (
     <Link to="/displaylist" style={{textDecoration:'none'}} key={index} > 
      <li className=" w3-hover-green w3-padding-16" onClick={this.handleClick(item.id)}> 
       <img src={require('./3.jpg')} className="w3-left w3-circle w3-margin-right " width="60px" height="auto" /> 
       <span>{item.Firstname}</span><br/><br/> 
      </li> 
     </Link> 
     )} 
    )} 
</ul> 

这就是所谓的partial application,所以你初始化传递ID,它会返回将要执行onClick函数的回调。

+0

非常感谢 –