[英]How do I prerender a namespaced react component using the react-rails gem?
(Note: I'm using the 'therubyracer', 'react-rails', and 'sprockets-coffee-react' gems) (注意:我正在使用'therubyracer','react-rails'和'sprockets-coffee-react'宝石)
This is the code for my simple component (Hello.js.cjsx): 这是我的简单组件(Hello.js.cjsx)的代码:
# @cjsx React.DOM
Hello = React.createClass(
render: ->
<div>
Hello {@props.name || "World"}!
</div>
)
window.components ?= {}
window.components.Hello = Hello
In my rails view (index.html.erb) this works just fine: 在我的rails视图(index.html.erb)中,这很好用:
<%= render_component('components.Hello', {name: 'Jack'}) %>
However, when I try this: 但是,当我尝试这个:
<%= react_component('components.Hello', {name: 'Jill'}, {prerender: true}) %>
I get this error: 我收到此错误:
Encountered error "ReferenceError: components is not defined"
which seems odd because I'm defining it in my component. 这看起来很奇怪,因为我在我的组件中定义它。
What am I doing wrong? 我究竟做错了什么?
The problem is that for anything rendered with render_component
it's required that the component is registered with the global window
object. 问题是,对于使用
render_component
渲染的任何内容,需要使用全局window
对象注册该组件。 This is kind of not ideal but just how it works for the time being. 这有点不理想,但它暂时如何运作。
This is what I've been doing. 这就是我一直在做的事情。 Not ideal, but helps.
不理想,但有帮助。
components /
namespace /
MyComponent.js.jsx
Then doing this in my file: 然后在我的文件中执行此操作:
# components/namespace/MyComponent.js.jsx
window.NamespaceMyComponent = React.createClass({});
module.exports = window.NamespaceMyComponent;
This last part allows me to use browserify and require my module like this: 最后一部分允许我使用browserify并要求我的模块如下:
require('components/namespace/MyComponent')
and using the render_component
helper like this: 并像这样使用
render_component
助手:
<%= render_component "NamespaceMyComponent", {}, {prerender: true} %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.