Search code examples
ruby-on-railsrubycoffeescriptreactjsreact-rails

Reactjs-rails troubles with pre-rendering


I use react-rails gem and met specific trouble: prerendering doesn't work. I've wrote the code, but send an exception to me:

Encountered error "ReferenceError: Terminal is not defined" when prerendering Terminal with {}

Here is the sources of my code:

#= require jquery
#= require jquery_ujs
#= require turbolinks
#= require react
#= require react_ujs
#= require components
#= require_tree .

components.js.coffee

#= require_tree ./components

terminal.js.jsx.coffee

Terminal = React.createClass
  render: ->
    `<div>fffs</div>`

And the view:

= react_component 'Terminal', {}, prerender: true

I'm using default react-rails settings and don't know what's going wrong (I can't understand why react can't find a Terminal component).


Solution

  • Your using prerender: true for server rendering, therefore your need to make sure your component is globally accessible:

    @Terminal = React.createClass
      render: ->
        `<div>fffs</div>`
    

    Read more about it on react-rails documentation.