Before mark as duplicate, Its differents. I have this scenario, where I suppose to get some non-predefined element into a child component. In where when the user clicks on that element it's fired on the parent element events. How can I stop the event trigger on the parent when the child is clicked? Let me explain more with the following example-
So, it's basically an accordion list, by clicking on each item title div (list-container__item____title
) the body div (list-container__item____body
) will display.
Now, inside the title div, I have a link (list-container__item____title--link
) which opens an overlay (child component). The overlay content comes from API call and the contains of it is HTML. I can't add some more functions for that element as I don't know what was included there, it's based on user choice. If in the HTML have some clickable element like an anchor, button, etc. the state (activeItem
) is updated with empty (''
) value. The result is the respective item is collapsed. Is there any way to prevent this event?
import React from 'react';
class DiamondFilters extends React.Component {
constructor(props) {
super(props);
this.state = {
activeItem: ''
};
}
handleItemToggle = (name) => {
if (this.state.activeItem === name) {
this.setState({ activeItem: '' })
} else {
this.setState({ activeItem: name })
}
}
render() {
return (
<div className="menu">
<div className="list-container">
<div className={`list-container__item ${this.state.activeItem === "item1" ? 'active' : ''}`}>
<div className="list-container__item____title" onClick={() => this.handleItemToggle('item1')}>
<a className="list-container__item____title--link">Click Here 1</a>
<OverlayModal modalType="full" modalName="item1" />
</div>
<div className="list-container__item____body">
<p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable.</p>
</div>
</div>
<div className={`list-container__item ${this.state.activeItem === "item2" ? 'active' : ''}`}>
<div className="list-container__item____title" onClick={() => this.handleItemToggle('item2')}>
<a className="list-container__item____title--link">Click Here 2</a>
<OverlayModal modalType="full" modalName="item2" />
</div>
<div className="list-container__item____body">
<p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable.</p>
</div>
</div>
</div>
</div>
);
}
}