Don Giulio
Don Giulio

Reputation: 3294

Uncaught ReferenceError: require is not defined react-rails

I'm trying to integrate react-rails 2.6.1 within my rails 4.2.11 app.

initially started with webpacker but it's giving me troubles with the actual prod deployment, so I removed webpacker and just pack stuff with sprockets 3.7.2.

The page is loaded properly but the react_component is not shown.

in the html source it's just rendered as a div:

<div data-react-class="Sidebar" data-react-props="..." data-react-cache-id="Sidebar-0"></div>

but nothing is shown.

in the console I can see:

Uncaught ReferenceError: require is not defined

which causes later on:

Cannot find component: 'Sidebar'. Make sure your component is available to render.

this is the definition of my react component:

import React from "react"

class Sidebar extends React.Component {
  render() {
    return (<p> hello from react </p> );
  }
}

I've tried moving imports from application.js up and down, (according to https://github.com/reactjs/react-rails/wiki/Troubleshooting) to no help.

Upvotes: 0

Views: 627

Answers (1)

Borni.Mr
Borni.Mr

Reputation: 679

Try to export your component to be able to import it

import React from "react"

class Sidebar extends React.Component {
  render() {
    return (<p> hello from react </p> );
  }
}

export default Sidebar;

Upvotes: 1

Related Questions