reactjs / react-rails

Integrate React.js with Rails views and controllers, the asset pipeline, or webpacker.
Apache License 2.0
6.75k stars 759 forks source link

browserify requrie() on react component #368

Closed josephMG closed 9 years ago

josephMG commented 9 years ago

Hi everyone,

I follow http://codeloveandboards.com/blog/2014/09/10/rails-and-react-ii-a-real-use-case/. They use the line cx = React.addons.classSet in the last step "The PersonCard component".

I want to change classSet to classnames, so I use browserify to let me use require('classname').

So I run npm install classnames and I simply change cx = React.addons.classSet to cx = require('classnames'). I got the error message: Uncaught ReferenceError: PersonCard is not defined

This is part of my Gemfile:

......
gem 'react-rails', github: 'reactjs/react-rails'
gem 'sprockets-coffee-react' 
gem "browserify-rails" 
......

and part of my package.json:

...
 "dependencies": {
   "browserify": "~> 10.2.4",
   "browserify-incremental": "^3.0.1",
   "coffeeify": "^1.1.0",
   "reactify": "^1.1.0"
 },
...

Original person_card.js.coffee and PersonCard.js output is in jsfiddle

I also followed #181 , but it doesn't help. Can anyone help me?

josephMG commented 9 years ago

Finally, I put window.classNames = require('classnames') to the end of components.js And I use cx = window.classNames in the coffeeScript file