Collin Allen
Collin Allen

Reputation: 4585

React events not firing

While working with <img> tags and getting familiar with React, I found that my onLoad and onError events weren't firing. After simplifying my usage to even the most basic React component, I'm still finding that events aren't firing.

In the following component, I'd expect that clicking the rendered <div> to log to the console. It renders fine, but upon clicking it, nothing happens. Am I missing something?

I'm using React 0.12.2 under Chrome 40.0.2214.111 on OS X 10.10.2, with code packed by webpack 1.5.3 and jsx-loader 0.12.2.

/** @jsx React.DOM */

module.exports = (function () {
  'use strict';
  var React = require('react');
  var SomeComponent = React.createClass({
    displayName: 'SomeComponent',
    doSomething: function (e) {
      console.log('did something');
    },
    render: function () {
      return (
        /* jshint ignore:start */
        <div onClick={this.doSomething}>Do Something</div>
        /* jshint ignore:end */
      );
    }
  });
  return SomeComponent;
})();

Upvotes: 2

Views: 1944

Answers (1)

Collin Allen
Collin Allen

Reputation: 4585

I finally figured it out!

In example.jsx, I realized I wasn't require()ing React before using it:

var React = require('react');
var SomeComponent = require('./some-component.jsx');
React.render( ... );

It seems so obvious in hindsight.

Upvotes: 2

Related Questions