React::ServerRendering::PrerenderError in... Rails 5, React

Viewed 933

I am using Rails 5 with react-rails gem. I want to use server-side rendering, but I see this error:

React::ServerRendering::PrerenderError in Home#index

Encountered error "# ExecJS::ProgramError: TypeError: Cannot read property 'serverRender' of undefined" when prerendering Main with {}

This is my /assets/javascripts/application.js:

//= require rails-ujs
//= require jquery
//= require react
//= require react_ujs
//= require_tree .

This is javascripts/components.jsx:

class Main extends React.Component{
    render(){
        return (
            <h1>Hello</h1>
        );
    }
}

and this is the view:

<%= react_component('Main', {}, {prerender: true}) %>

Without prerender option, everything works.

1 Answers

I've had a similar issue and here's how I solved it

run command rails generate react:install

This will create the components folder and some required files under your javascripts/ dir.

Now, place your components.jsx in /assets/javascripts/componets directory

refresh the page.

I'm running rails 4.2.10 and haven't tested rails 5 but I'm guessing this should do the trick.

Let me know how you get on

Related