Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
137
rated 0 times [  143] [ 6]  / answers: 1 / hits: 62727  / 8 Years ago, sat, july 9, 2016, 12:00:00

enter


I now have a function handleRightClick(e) which will be called when I right click on the container. Inside the container, there are several Items and I expect the menu will be shown only when I right click one of the Items.


export default class ProjectContainer extends React.Component {
...
handleRightClick(e) {
console.log(e.target.name); // I want to check the event target whether is `Item` Class.
this.refs.rightClickMenu.reShow(e.clientX, e.clientY); // This will open the right click menu.
}
...
render() {
return (
<div style={styles.root} onContextMenu={this.handleRightClick} onClick={this.handleLeftClick}>
<Item /><Item /><Item /><Item /><Item /><Item /><Item />
<RightClickMenuForProjectItem ref='rightClickMenu'/>
</div>
);
}
}

If I console.log(e), I get this in Chrome console:


> Object {dispatchConfig: Object, _targetInst: ReactDOMComponent, _dispatchInstances: ReactDOMComponent, nativeEvent: MouseEvent, type: "contextmenu"…}

This is the class Item:


export default class Item extends React.Component {
render() {
return (
<Card style={styles.card} onClick={this.props.onClick}>
<img style={styles.img}/>
<div style={styles.divInfo}>
<h4 style={styles.title}>{this.props.title}</h4>
<div style={styles.projectType}>{this.props.projectType}</div>
</div>
</Card>
);
}
}

Finally, I will use it to form something like this:


handleRightClick(e) {
if (e.target.className == "Item") {
// Open the right click menu only when I right click one of the Item.
this.refs.rightClickMenu.reShow(e.clientX, e.clientY);
}
}

I want to check the event target whether is Item class. How can I access the class name of the event target?


More From » reactjs

 Answers
20

To access at className an element use e.target.className



Try with this



export default class ProjectContainer extends React.Component {
...
handleRightClick(e) {
// To avoid get wrong class name, use this.
// But if the default context menu come up, without this is OK.
e.stopPropagation()
console.log(e.target.className); // This get the className of the target
this.refs.rightClickMenu.reShow(e.clientX, e.clientY);
}
...
}


This is the same on javascript without lib's



If an empty result is appeared in the console, this means that you haven't set the className of the Item class in the render return. You can change your class to be like this:



const className = 'Item';
export default class Project extends React.Component {
...
render() {
return (
<Card style={styles.card} onClick={this.props.onClick} className={className}>
<img style={styles.img} className={className}/>
<div style={styles.divInfo} className={className}>
<h4 style={styles.title} className={className}>{this.props.title}</h4>
<div style={styles.projectType} className={className}>{this.props.projectType}</div>
</div>
</Card>
);
}
}


Now the resulting handleRightClick(e) should be like this:



handleRightClick(e) {
if (e.target.className == 'Item')
//Show the menu if it is not visible, reShow the menu if it is already visible
this.refs.rightClickMenu.reShow(e.clientX, e.clientY);
else
//Hide the menu
this.refs.rightClickMenu.hide();
}


Result



The menu will be shown when click one of the Item.

enter



The menu will not be shown when click outside the Item.

enter


[#61443] Thursday, July 7, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kamronr

Total Points: 749
Total Questions: 110
Total Answers: 122

Location: Dominica
Member since Sat, Nov 5, 2022
2 Years ago
kamronr questions
Mon, Dec 21, 20, 00:00, 3 Years ago
Fri, Oct 16, 20, 00:00, 4 Years ago
Sat, Oct 3, 20, 00:00, 4 Years ago
Sun, Jul 28, 19, 00:00, 5 Years ago
;