user629283 user629283 - 1 year ago 68
React JSX Question

React: trying to add an onClick to a li tag but the click handler function is undefined

I am new to react, so I don't know much about it.
I am trying to add a click handler to a li but the function seems to be undefined?

var ActivityList = React.createClass({
getInitialState: function() {
return {name:"test"};
return (
return <li onClick={this.handleClick} name={}>{}</li>;

I am wondering if it is because i have my scoping wrong here?

Answer Source

Set this to .map, because in .map callback this refers to global scope(in browser it is window or undefined if you use strict mode) {
  return <li onClick={this.handleClick} name={}>{}</li>;
}, this);


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download