Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,11 @@ Alternatively, you can include it directly as a separate script tag:

### JSX

To transform your JSX into JS, simply create `.js.jsx` files, and ensure that the file has the `/** @jsx React.DOM */` docblock. These files will be transformed on request, or precompiled as part of the `assets:precompile` task.
To transform your JSX into JS, simply create `.js.jsx` files. These files will be transformed on request, or precompiled as part of the `assets:precompile` task.

CoffeeScript files can also be used, by creating `.js.jsx.coffee` files. You must use this form of the docblock at the top of each file: `###* @jsx React.DOM ###`. We also need to embed JSX inside backticks so CoffeeScript ignores the syntax it doesn't understand. Here's an example:
CoffeeScript files can also be used, by creating `.js.jsx.coffee` files. We also need to embed JSX inside backticks so CoffeeScript ignores the syntax it doesn't understand. Here's an example:

```coffee
###* @jsx React.DOM ###

Component = React.createClass
render: ->
`<ExampleComponent videos={this.props.videos} />`
Expand Down Expand Up @@ -191,8 +189,6 @@ In order for us to render your React components, we need to be able to find them
This will bring in all files located in the `app/assets/javascripts/components` directory. You can organize your code however you like, as long as a request for `/assets/javascripts/components.js` brings in a concatenated file containing all of your React components, and each one has to be available in the global scope (either `window` or `global` can be used). For `.js.jsx` files this is not a problem, but if you are using `.js.jsx.coffee` files then the wrapper function needs to be taken into account:

```coffee
###* @jsx React.DOM ###

Component = React.createClass
render: ->
`<ExampleComponent videos={this.props.videos} />`
Expand Down Expand Up @@ -258,8 +254,6 @@ end
It is possible to use JSX with CoffeeScript. The caveat is that you will still need to include the docblock. Since CoffeeScript doesn't allow `/* */` style comments, we need to do something a little different. We also need to embed JSX inside backticks so CoffeeScript ignores the syntax it doesn't understand. Here's an example:

```coffee
###* @jsx React.DOM ###

Component = React.createClass
render: ->
`<ExampleComponent videos={this.props.videos} />`
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_3.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ source "http://rubygems.org"
gem "rails", "~> 3.1"
gem "sprockets", ">= 2.2.2"

gemspec :path=>"../"
gemspec :path => "../"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These gemfiles were autogenerated. Did you fix them manually or does an updated appraisal install do it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't edit them manually, they were added by appraisal.

2 changes: 1 addition & 1 deletion gemfiles/rails_3.2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ source "http://rubygems.org"

gem "rails", "~> 3.2"

gemspec :path=>"../"
gemspec :path => "../"
2 changes: 1 addition & 1 deletion gemfiles/rails_4.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ source "http://rubygems.org"

gem "rails", "~> 4.0"

gemspec :path=>"../"
gemspec :path => "../"
4 changes: 2 additions & 2 deletions gemfiles/rails_4.0_with_therubyracer.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
source "http://rubygems.org"

gem "rails", "~> 4.0"
gem "therubyracer", "0.12.0", :platform=>:mri
gem "therubyracer", "0.12.0", :platform => :mri

gemspec :path=>"../"
gemspec :path => "../"
2 changes: 1 addition & 1 deletion gemfiles/rails_4.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ source "http://rubygems.org"

gem "rails", "~> 4.1"

gemspec :path=>"../"
gemspec :path => "../"
2 changes: 1 addition & 1 deletion lib/assets/javascripts/react_ujs.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
var constructor = window[className] || eval.call(window, className);
var propsJson = node.getAttribute(PROPS_ATTR);
var props = propsJson && JSON.parse(propsJson);
React.renderComponent(constructor(props), node);
React.render(React.createElement(constructor, props), node);
}
};

Expand Down
2 changes: 1 addition & 1 deletion lib/react/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def render(component, args={})
react_props = React::Renderer.react_props(args)
jscode = <<-JS
function() {
return React.renderComponentToString(#{component}(#{react_props}));
return React.renderToString(React.createElement(#{component}, #{react_props}));
}()
JS
context.eval(jscode).html_safe
Expand Down
2 changes: 1 addition & 1 deletion react-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Gem::Specification.new do |s|

s.add_dependency 'execjs'
s.add_dependency 'rails', '>= 3.1'
s.add_dependency 'react-source', '0.11.1'
s.add_dependency 'react-source', '0.12'
s.add_dependency 'connection_pool'

s.files = Dir[
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
###* @jsx React.DOM ###

Todo = React.createClass
render: ->
`<li>{this.props.todo}</li>`
Expand Down
3 changes: 0 additions & 3 deletions test/dummy/app/assets/javascripts/components/TodoList.js.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/** @jsx React.DOM */

TodoList = React.createClass({
getInitialState: function() {
return({mounted: "nope"});
Expand All @@ -18,4 +16,3 @@ TodoList = React.createClass({
)
}
})

1 change: 0 additions & 1 deletion test/dummy/app/assets/javascripts/example.js.jsx
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
/** @jsx React.DOM */
<div/>;
2 changes: 0 additions & 2 deletions test/dummy/app/assets/javascripts/example2.js.jsx.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
###* @jsx React.DOM ###

Component = React.createClass
render: ->
`<ExampleComponent videos={this.props.videos} />`
1 change: 0 additions & 1 deletion test/dummy/app/assets/javascripts/example3.js.jsx
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
/** @jsx React.DOM */
<div/>;
3 changes: 0 additions & 3 deletions test/helper_files/TodoListWithUpdates.js.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/** @jsx React.DOM */

TodoList = React.createClass({
getInitialState: function() {
return({mounted: "nope"});
Expand All @@ -19,4 +17,3 @@ TodoList = React.createClass({
)
}
})

14 changes: 4 additions & 10 deletions test/jsxtransform_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,16 @@

# Sprockets is inserting a newline after the docblock for some reason...
EXPECTED_JS = <<eos
/** @jsx React.DOM */

React.DOM.div(null);
React.createElement("div", null);
eos

EXPECTED_JS_2 = <<eos
/** @jsx React.DOM */

(function() {
var Component;

Component = React.createClass({displayName: 'Component',
render: function() {
return ExampleComponent( {videos:this.props.videos} );
return React.createElement(ExampleComponent, {videos:this.props.videos} );
}
});

Expand All @@ -35,10 +31,8 @@ class JSXTransformTest < ActionDispatch::IntegrationTest
test 'asset pipeline should transform JSX + Coffeescript' do
get 'assets/example2.js'
assert_response :success
# Different coffee-script may generate slightly different outputs:
# 1. Some version inserts an extra "\n" at the beginning.
# 2. "/** @jsx React.DOM */" and "/** @jsx React.DOM*/" are both possible.
#
# Different coffee-script may generate slightly different outputs,
# as some version inserts an extra "\n" at the beginning.
# Because appraisal is used, multiple versions of coffee-script are treated
# together. Remove all spaces to make test pass.
assert_equal EXPECTED_JS_2.gsub(/\s/, ''), @response.body.gsub(/\s/, '')
Expand Down
10 changes: 6 additions & 4 deletions test/react_renderer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ class ReactRendererTest < ActiveSupport::TestCase
err = assert_raises React::Renderer::PrerenderError do
React::Renderer.render("NonexistentComponent", {error: true, exists: false})
end
expected_message = 'Encountered error "ReferenceError: NonexistentComponent is not defined" when prerendering NonexistentComponent with {"error":true,"exists":false}'
assert_equal expected_message, err.message
expected_message_one = 'Encountered error "ReferenceError: Can\'t find variable: NonexistentComponent" when prerendering NonexistentComponent with {"error":true,"exists":false}'
expected_message_two = 'Encountered error "ReferenceError: NonexistentComponent is not defined" when prerendering NonexistentComponent with {"error":true,"exists":false}'
assert (expected_message_one == err.message || expected_message_two == err.message)
end

test 'prerender errors are thrown when given a string' do
Expand All @@ -43,7 +44,8 @@ class ReactRendererTest < ActiveSupport::TestCase
err = assert_raises React::Renderer::PrerenderError do
React::Renderer.render("NonexistentComponent", json_string)
end
expected_message = 'Encountered error "ReferenceError: NonexistentComponent is not defined" when prerendering NonexistentComponent with {"error":true,"exists":false}'
assert_equal expected_message, err.message
expected_message_one = 'Encountered error "ReferenceError: Can\'t find variable: NonexistentComponent" when prerendering NonexistentComponent with {"error":true,"exists":false}'
expected_message_two = 'Encountered error "ReferenceError: NonexistentComponent is not defined" when prerendering NonexistentComponent with {"error":true,"exists":false}'
assert (expected_message_one == err.message || expected_message_two == err.message)
end
end