0

react-rails在项目中使用 gem。

每当我将选项传递给辅助方法prerender: true的选项哈希时,我都会收到错误: . 当我从选项哈希中删除时,我的组件工作正常。react_componentV8::Error - Unexpected token <prerender: true

宝石文件:

gem 'rails', '4.1.1'
gem 'execjs'
gem 'therubyracer', platforms: :ruby
gem 'react-rails', github: 'reactjs/react-rails'

风景:

= react_component("AssignmentWindowProgressBar", { assignment: @assignment_json }, { prerender: true })

咖啡档案:

###* @jsx React.DOM ###

@AssignmentWindowProgressBar = React.createClass
  render: ->
    `<div>Hi world.</div>`

# this is located in this file:
# ./apps/assets/javascripts/components/assignments/AssignmentWindows.js.jsx.coffee

堆栈跟踪:

V8::Error - Unexpected token < at <eval>:19037:15:
  therubyracer (0.12.1) lib/v8/error.rb:86:in `block in try'
  therubyracer (0.12.1) lib/v8/error.rb:83:in `try'
  therubyracer (0.12.1) lib/v8/context.rb:95:in `block in eval'
  therubyracer (0.12.1) lib/v8/context.rb:248:in `block (2 levels) in lock_scope_and_enter'
  therubyracer (0.12.1) lib/v8/context.rb:245:in `block in lock_scope_and_enter'
  therubyracer (0.12.1) lib/v8/context.rb:244:in `lock_scope_and_enter'
  therubyracer (0.12.1) lib/v8/context.rb:204:in `enter'
  therubyracer (0.12.1) lib/v8/context.rb:94:in `eval'
  execjs (2.2.0) lib/execjs/ruby_racer_runtime.rb:11:in `block in initialize'
  execjs (2.2.0) lib/execjs/ruby_racer_runtime.rb:78:in `block in lock'
  execjs (2.2.0) lib/execjs/ruby_racer_runtime.rb:76:in `lock'
  execjs (2.2.0) lib/execjs/ruby_racer_runtime.rb:9:in `initialize'
  execjs (2.2.0) lib/execjs/runtime.rb:44:in `compile'
  execjs (2.2.0) lib/execjs/module.rb:27:in `compile'
  ... end of execjs errors ...

感谢您的所有帮助!

4

1 回答 1

0

从https://github.com/reactjs/react-rails#jsx复制答案

要将 JSX 转换为 JS,只需创建 .js.jsx 文件。这些文件将根据要求进行转换,或作为 assets:precompile 任务的一部分进行预编译。

通过创建 .js.jsx.coffee 文件,也可以使用 CoffeeScript 文件。我们还需要在反引号中嵌入 JSX,以便 CoffeeScript 忽略它不理解的语法。这是一个例子:

Component = React.createClass
  render: ->
    `<ExampleComponent videos={this.props.videos} />`

注意文件扩展名。

于 2014-12-21T18:49:14.080 回答