pass id through on click react.js

前端 未结 6 1570
暖寄归人
暖寄归人 2020-12-08 16:52

Below is my partial code but my question is very simple, how can I get says data-id=\"1\" to my function when user clicked on the li?

render(){
         


        
6条回答
  •  猫巷女王i
    2020-12-08 17:22

    You can do this as follows :

    class Test extends React.Component {
        constructor(props){
             super(props);
             this.state = {
                items: [
                    {item: "item", id: 1},
                    {item1: "item1", id: 2}
                ]
             }
        }
    
        handleClick(id, e){
            alert(id);
        }
    
        render(){
            return(
               
      {this.state.items.map((item,i) =>
    • {item.name}
    • )}
    ) } } React.render(, document.getElementById('container'));

    Here is jsfiddle.

提交回复
热议问题