I have a situation when I have to render React controls in some code that I do not control. It's a kendo UI grid and I need to render custom React component in the grid cells.
Grid is created using jQuery under the hood. It also addes a click
event listener for the parent html element (on the table row). In my React component I also have a click
event listener. The problem is that the handler attached to the parent fires first.
The Snippet below (also available as a fiddle) demonstrates the issue. To keep this example as simple as possible I just use a div
instead of the Kendo grid. The point is we have an event handler on the parent html element, then we render the React component into this parent element with its click
handler, and the parent element takes precedence over React.
class Btn extends React.Component {
constructor(props) {
super(props);
this.click = this.click.bind(this);
}
click() {
alert('Btn');
}
render() {
return (
<span onClick={this.click}>Click me</span>
);
}
};
$(function() {
$('#container').on('click', function() {
alert('JS');
});
ReactDOM.render(
<Btn />,
document.getElementById('btn')
);
});
<div id="container">
<div id="btn">
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>