tajihiro
tajihiro

Reputation: 2443

How to recognize which link is clicked with JSX. - React

I have a lot of <a href />. I would like to recognize which link is clicked.

    onClick(e){
        e.preventDefault();
        console.log("CLICK!!" + e);
    }

    render(){
      <a key={member.id} className="dropdown-item" href="#" onClick={this.onClick}>
        {member.name}
      </a>
    }

How can I do for it? Thanks.

Upvotes: 0

Views: 31

Answers (1)

Anthony Poon
Anthony Poon

Reputation: 887

You can do something like this:

<a href onClick={evt => { this.onClick("a_1", evt)}}/>

onClick(name, evt) {
    switch (name) {
        case "a_1":
            // ...
            break;
        case "a_2":
            // ...
            break;
    }
}

Upvotes: 1

Related Questions