React js onClick can't pass value to method

后端 未结 30 1703
北荒
北荒 2020-11-22 07:05

I want to read the onClick event value properties. But when I click on it, I see something like this on the console:

SyntheticMouseEvent {dispatchConfig: Ob         


        
30条回答
  •  终归单人心
    2020-11-22 07:42

    this example might be little different from yours. but i can assure you that this is the best solution you can have for this problem. i have searched for days for a solution which has no performance issue. and finally came up with this one.

    class HtmlComponent extends React.Component {
      constructor() {
        super();
        this.state={
           name:'MrRehman',
        };
        this.handleClick= this.handleClick.bind(this);
      }
    
      handleClick(event) {
        const { param } = e.target.dataset;
        console.log(param);
        //do what you want to do with the parameter
      }
    
      render() {
        return (
          

    {this.state.name}

    ); } }

    UPDATE

    incase you want to deal with objects that are supposed to be the parameters. you can use JSON.stringify(object) to convert to it to string and add to the data set.

    return (
       

    {this.state.name}

    );

提交回复
热议问题