Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How use react-rails with webpacker?

I created example project react-rails with webpacker. but show error "Uncaught ReferenceError: Person is not defined". help me How use react-rails with webpacker? The processing I performed is as follows.

Rails version

$ rails -v
Rails 5.0.2

Gem version

react-rails (2.1.0)
webpacker (1.1)

Create Rails project

$ rails new example

Add webpacker and react-rails to Gemfile

gem 'webpacker', github: 'rails/webpacker'
gem "react-rails"

Install webpacker and react

$ rails webpacker:install
$ rails webpacker:install:react
$ rails generate react:install

Create controller

$ bin/rails g controller home

Add route

config/routes.rb
root "home#index"

Add react component

$ bin/rails g react:component person name --es6

Add react_component tag

app/views/home/index.html.erb
<%= react_component("Person", { name: "Hello" }) %>

Add javascript_pack_tag to application.html.erb

app/views/layouts/application.html.erb
<%= javascript_pack_tag 'application' %>

Run server

$ bin/rails s
$ bin/webpack-dev-server

Show error console

VM27375:1 Uncaught ReferenceError: Person is not defined
    at eval (eval at module.exports (fromGlobal.js?fc31:13), <anonymous>:1:1)
    at module.exports (fromGlobal.js?fc31:13)
    at Object.getConstructor (fromRequireContextWithGlobalFallback.js?cbbb:16)
    at Object.mountComponents (index.js?c0e8:82)
    at HTMLDocument.ReactRailsUJS.handleMount (index.js?c0e8:124)
    at HTMLDocument.dispatch (jquery.self-bd7ddd3….js?body=1:5227)
    at HTMLDocument.elemData.handle (jquery.self-bd7ddd3….js?body=1:4879)

example project

https://github.com/yokochi/webpacker_example

like image 711
Akio Yamazaki Avatar asked Apr 22 '17 10:04

Akio Yamazaki


People also ask

How do I integrate Ruby on Rails With React?

There are two ways to use React with Ruby on Rails. The first is to build two separate, standalone apps, with the React app on a different repository and communicating with the backend via an API. This can simply be achieved by creating both apps separately using Create React App and the Rails CLI.

Can we use React with Rails?

react-rails is the official React community gem for integrating React with Rails. The main benefit of using this gem is the react_component helper method which makes it easy to pass Rails data to components. You can use react-rails with webpacker or with Sprockets (to bundle the JSX into the asset pipeline).

Should I use Ruby on Rails with React?

It's easier and faster to ship your MVP with Ruby on Rails and React. Use RoR for the back-end and React for the front end creating interactive UIs. Enjoy stability — RoR is backed by a fantastic community and there are gems for what not. React comes from Facebook and the APIs don't change much.


2 Answers

Either rename app/javascript/components/person.js to app/javascript/components/Person.js (e.g. capitalize)

Or use react_component('person') instead.

TLDR; capitalization matters.

like image 82
Rystraum Avatar answered Nov 08 '22 16:11

Rystraum


@Akio Yamazaki

https://github.com/yokochi/webpacker_example/commit/55ca96c1257e611dfb17b27fe53b402a97d5dedc#diff-0c7c7cb70dec34ec9dcff19a0f5bd40bR1

you should require('react') in your component and export component

try this...

var React = require("react")

class Person extends React.Component {
  render () {
    return (
      <div>
        <div>Name: {this.props.name}</div>
      </div>
    );
  }
}

Person.propTypes = {
  name: React.PropTypes.node
};

module.exports = Person

but this way be better

import React from 'react'

class Person extends React.Component {
  constructor(props){
    super(props)
  }

  render() {
    <div>Name: {this.props.name}</div>
  }
}

export default Person

or...

import React from 'react'

const Person = props => (
  <div>Name: {props.name}</div>
)

export default Person

I have a repo maybe able to give some reference

https://github.com/kakas/react-rails_with_webpacker_example/commits/master

like image 2
kakas Avatar answered Nov 08 '22 16:11

kakas