Try with the following code:
componentDidMount() {
document.addEventListener("click", this.navButtonListener);
}
componentWillUnmount() {
document.removeEventListener("click", this.navButtonListener);
}
navButtonListener = (e) => {
if(e.target.id == "wl-nav-button"){
this.handleClick();
}
}
What this does is it adds an event listener to the entire document, which is fired anytime you click. Then, a check is done to see what the clicked items id attribute is set to. If the id is your button, it fires the handleClick() function.
This also removes the event listener from your component before un-mounting it.
Demo
Here is a simple demo that just demonstrates how to add an event handler in React to something that was not rendered with React.
class MyApp extends React.Component {
componentDidMount() {
document.addEventListener("click", this.navButtonListener);
}
componentWillUnmount() {
document.removeEventListener("click", this.navButtonListener);
}
navButtonListener = (e) => {
if (e.target.id == "wl-nav-button") {
this.handleClick();
}
}
handleClick() {
console.log("click!!");
}
render() {
return (
<div>
<p>A button from a simulated external non-react library will be inserted asynchronically below:</p>
<div id="my-container"></div>
</div>
);
}
}
ReactDOM.render(<MyApp />, document.getElementById("myApp"));
//simulate a non-react library asynchronically inserting into the DOM:
setTimeout(function() {
var button = document.createElement("BUTTON");
var text = document.createTextNode("Click me!");
button.appendChild(text);
button.setAttribute("id", "wl-nav-button");
document.getElementById("my-container").appendChild(button);
}, 2000);
<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>
<div id="myApp"></div>
Module build failed: SyntaxError: Unexpected token, expected ( (74:10)on document.get...onClickin the element as it's creating dynamically by the widget script