2017-02-11 48 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

非常感謝 –