I have a React Component that renders a <ul>
and inserts <li>
elements based on state.
class SimpleComponent extends React.Component {
constructor(props) {
this.state = { menu_items: [name: "First", id: 10] }
this.clickMenu = this.clickMenu.bind(this)
}
generateLinks() {
let to_return='';
for (var i=0;i<this.state.menu_items.length; i++) {
to_return = to_return + `<li><a onclick={clickMenu}> ${this.state.menu_item[i]['name']} </a></li>`
}
return to_return;
}
clickMenu(e) {
console.log(e.target)
}
render() {
return(
<ul dangerouslySetInnerHTML={this.generateLinks()}></ul>
)
}
}
When I click on the Anchor, the console indicates Uncaught ReferenceError: clickMenu not defined. I've tried using this.clickMenu instead, but nothing occurs. I've noticed that the rendered anchor looks like:
<a onclick="{menuClick}">
Is there a way to create these anchor elements to have React pick up the onClick definitions rather than passing them to the browser to interpret?