简体   繁体   English

如何使用onClick将数据从.map传输到href元素的功能

[英]How to transfer data from .map to function from href element with onClick

I'm trying to move one element from .map to onClick function so that I can pass it to redux but I'm not sure how to proceed. 我正在尝试将一个元素从.map移到onClick函数,以便可以将其传递给redux,但是我不确定如何继续。 This is my code 这是我的代码

const Lists = lists.map((list, index) =>
    <li key={index}> {list.name} <a href="#" onClick={this.listInfo.bind(this)}>Learn More</a> </li> );

this is the function 这是功能

listInfo(event){
  event.preventDefault();
  var el = list.name //this is where I want to save that value;
  console.log(el)
}

Thanks in advance 提前致谢

Try this.... 尝试这个....

const Lists = lists.map((list, index) =>
<li key={index}> {list.name} <a href="#" onClick={(e) => this.listInfo(e, list.name)}>Learn More</a> </li> );

And change your function to this... 然后将功能更改为此...

listInfo(event, listName){
//.....
console.log(listName);
}

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

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