0

Here's a simple react file that handles click:

//src/react/components/list.js
/** @jsx React.DOM */
var React = require('react/addons');
var List = React.createClass({
  displayName: 'List',

  handleClick: function() {
    console.error('click');
    var resultData = {
      name: 'test',
      version: '0.0.1'
    };
  },

  render: function() {
    return (
      <div>
        List
        <button onClick={this.handleClick}>Add</button>
      </div>
    );
  }
});

module.exports = List;

Button renders with no problem but nothing happens when I clicked it. I suppose it means server-side rendering works but client-side rendering is not working? I'm using grunt and browserify, and this is my Gruntfile.js:

module.exports = function(grunt) {

  // Project configuration.
  grunt.initConfig({
    browserify: {
      client: {
        src: ['src/react/**/*.js'],
        dest: 'public/js/react/main.js'
      },
      options: {
        debug: true,
        transform: ['reactify']
      }
    },
    watch: {
      js: {
        files: ['src/react/**/*.js'],
        tasks: ['browserify']
      }
    }
  });

  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-browserify');
  grunt.registerTask('default', ['browserify', 'watch']);
};

And then I confirmed code in src/react/components/list.js is built into /public/js/react/main.js. So what could the problem be? Any help is appreciated!

odieatla
  • 1,049
  • 3
  • 15
  • 35

1 Answers1

0

http://plnkr.co/edit/lJ2aWzvcR2vQ6PVuneqK?p=preview

I tried it on plnkr, and it works well (open Chrome Console or Firebug to actually see that when you click the button, a console.error is thrown).

Maybe on your side is the require('react/addons') that is not working ? If you already have React as a global variable, this require is the source of your problems I think.

Tom
  • 300
  • 1
  • 9
  • Hi @Tom, I comment `var React = require('react/addons'); ` in List.js but I cannot start the server any more: ReferenceError: React is not defined. BTW, it is very weird that console.error() in componentDidMount didn't get called as well -- neither in server side log nor in Chrome's console. Thanks! – odieatla Jul 24 '15 at 06:38
  • Ok, just let the React import, it should be ok. I think you are mistaken, there is no `componentDidMount` in the code you provided here, but if there was a `componentDidMount` with a `console.error` in it, the message should be displayed on the browser's console. BTW, there is no server-side render here, the browserify + watch transform only 'serves' the JS distribution, and it is executed by your browser only. Glad to help :) – Tom Jul 24 '15 at 07:09
  • I added `componentDidMount ` to the code but I didn't see any error raised , in browser console or server log.. Well, there must be something fundamentally wrong with my setup.. – odieatla Jul 24 '15 at 18:17