pass id through on click react.js

前端 未结 6 1567
暖寄归人
暖寄归人 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条回答
  •  星月不相逢
    2020-12-08 17:27

    Mayid is correct that it is not good to declare or bind functions in render method if the function is passed into other component as a props.

    Unfortunately currentTarget didn't work for me.

    I have used getAttribute function of event.target. This doesn't cause unnecessary rerenders.

    class App extends React.Component {
      handleClick = (event) => {    
        console.log(event.target.getAttribute('index'))
      }
    
      render() {
        return (
          
        );
      }
    }
    

提交回复
热议问题