All Projects → renchap → Webpacker React

renchap / Webpacker React

Licence: mit
Webpacker plugin to integrate React in your Rails application

Programming Languages

javascript
184084 projects - #8 most used programming language
ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Webpacker React

Kickoff tailwind
A rapid Rails 6 application template for personal use bundled with Tailwind CSS
Stars: ✭ 287 (+42.79%)
Mutual labels:  webpack, rails
Webpacker
Use Webpack to manage app-like JavaScript modules in Rails
Stars: ✭ 5,282 (+2527.86%)
Mutual labels:  webpack, rails
Hyper React
The project has moved to Hyperstack!!
Stars: ✭ 295 (+46.77%)
Mutual labels:  webpack, rails
Hyperstack
Hyperstack ALPHA https://hyperstack.org
Stars: ✭ 463 (+130.35%)
Mutual labels:  webpack, rails
Npm Pipeline Rails
Use npm as part of your Rails asset pipeline
Stars: ✭ 93 (-53.73%)
Mutual labels:  webpack, rails
React webpack rails
Simple and lightweight react-webpack-rails integration.
Stars: ✭ 248 (+23.38%)
Mutual labels:  webpack, rails
Kails
A Web App like Ruby on Rails with Koa2, Webpack and Postgres
Stars: ✭ 512 (+154.73%)
Mutual labels:  webpack, rails
Docker Web Framework Examples
Example apps that demonstate how to use Docker with your favorite web frameworks.
Stars: ✭ 204 (+1.49%)
Mutual labels:  webpack, rails
Ifme
Free, open source mental health communication web app to share experiences with loved ones
Stars: ✭ 1,147 (+470.65%)
Mutual labels:  webpack, rails
Docker Rails React Starter
A basic docker-compose, Rails and React / Webpack starter kit
Stars: ✭ 52 (-74.13%)
Mutual labels:  webpack, rails
Vue Rails Form Builder Demo
An example of Rails app using vue-form-for gem
Stars: ✭ 12 (-94.03%)
Mutual labels:  webpack, rails
Vueport
Single file components for Rails with Vue JS and Webpack
Stars: ✭ 141 (-29.85%)
Mutual labels:  webpack, rails
Simpacker
Use modern JavaScript build system in Rails.
Stars: ✭ 100 (-50.25%)
Mutual labels:  webpack, rails
Limestone
Boilerplate Rails 6 SaaS application with Webpack, Stimulus and Docker integration.
Stars: ✭ 191 (-4.98%)
Mutual labels:  webpack, rails
Ansible Rails
Ruby on Rails deployment using Ansible - with Lets Encrypt, Sidekiq, PostgreSQL, nginx & puma
Stars: ✭ 199 (-1%)
Mutual labels:  rails
Webpack Nano
A teensy, squeaky 🐤 clean Webpack CLI
Stars: ✭ 199 (-1%)
Mutual labels:  webpack
Django Webpack Loader
Transparently use webpack with django
Stars: ✭ 2,327 (+1057.71%)
Mutual labels:  webpack
Webpack Dev Middleware
A development middleware for webpack
Stars: ✭ 2,336 (+1062.19%)
Mutual labels:  webpack
Webpackbin
[Deprecated]. Please move to codesandbox.io.
Stars: ✭ 200 (-0.5%)
Mutual labels:  webpack
Jamstack Web Starter
Static website workflow utilising Eleventy, Tailwind CSS, Webpack and PostCSS.
Stars: ✭ 198 (-1.49%)
Mutual labels:  webpack

Webpacker-React CircleCI

Note: This is the documentation for the Git master branch. Documentation for the latest release (1.0.0-beta.1) is available here.

Webpacker-React makes it easy to use React with Webpacker in your Rails applications.

It supports Webpacker 1.2+.

An example application is available: https://github.com/renchap/webpacker-react-example/

Installation

Your Rails application needs to use Webpacker and have the React integration done. Please refer to their documentation documentation for this: https://github.com/rails/webpacker/blob/master/README.md#ready-for-react

First, you need to add the webpacker-react gem to your Rails app Gemfile:

gem 'webpacker-react', "~> 1.0.0.beta.1"

Once done, run bundle to install the gem.

Then you need to update your package.json file to include the webpacker-react NPM module:

./bin/yarn add webpacker-react

You are now all set!

Note about versions

Webpacker-React contains two parts: a Javascript module and a Ruby gem. Both of those components respect semantic versioning. When upgrading the gem, you need to upgrade the NPM module to the same minor version. New patch versions can be released for each of the two independently, so it is ok to have the NPM module at version A.X.Y and the gem at version A.X.Z, but you should never have a different A or X.

Usage

The first step is to register your root components (those you want to load from your HTML). In your pack file (app/javascript/packs/*.js), import your components as well as webpacker-react and register them. Considering you have a component in app/javascript/components/hello.js:

import Hello from 'components/hello'
import WebpackerReact from 'webpacker-react'

WebpackerReact.setup({Hello}) // ES6 shorthand for {Hello: Hello}

With Turbolinks

You have to make sure Turbolinks is loaded before calling WebpackerReact.initialize().

For example:

import Hello from 'components/hello'
import WebpackerReact from 'webpacker-react'
import Turbolinks from 'turbolinks'

Turbolinks.start()

WebpackerReact.setup({Hello})

You may also load turbolinks in regular asset pipeline application.js:

//= require "turbolinks"

In that case, make sure your packs are loaded after application.js

Now you can render React components from your views or your controllers.

Rendering from a view

Use the react_component helper. The first argument is your component's name, the second one is the props:

<%= react_component('Hello', name: 'React') %>

You can pass a tag argument to render the React component in another tag than the default div. All other arguments will be passed to content_tag:

<%= react_component('Hello', { name: 'React' }, tag: :span, class: 'my-custom-component') %>
# This will render <span class="my-custom-component" data-react-class="Hello" data-react-props="..."></span>

Rendering from a controller

class PageController < ApplicationController
  def main
    render react_component: 'Hello', props: { name: 'React' }
  end
end

You can use the tag_options argument to change the generated HTML, similar to the react_component method above:

render react_component: 'Hello', props: { name: 'React' }, tag_options: { tag: :span, class: 'my-custom-component' }

You can also pass any of the usual arguments to render in this call: layout, status, content_type, etc.

Note: you need to have Webpack process your code before it is available to the browser, either by manually running ./bin/webpack or having the ./bin/webpack-watcher process running.

Hot Module Replacement

HMR allows to reload / add / remove modules live in the browser without reloading the page. This allows any change you make to your React components to be applied as soon as you save, preserving their current state.

  1. install react-hot-loader (version 4):

    ./bin/yarn add [email protected]
    
  2. update your Babel or Webpack config. We provide a convenience function to add the necessary changes to your config if it's not significantly different than the standard Webpacker config:

    // config/webpack/development.js
    // This assumes Webpacker 3+
    
    const environment = require("./environment")
    const webpackerReactconfigureHotModuleReplacement = require('webpacker-react/configure-hot-module-replacement')
    
    const config = environment.toWebpackConfig()
    
    module.exports = webpackerReactconfigureHotModuleReplacement(config)
    

    If you prefer to do it manually, you need to add react-hot-loader/babel in your Babel plugins (in your .babelrc or .babelrc.js). You can include it only for development.

  3. once Babel is configured, webpack-dev-server needs to be set up for HMR. This is easy, just switch hmr: true in your webpacker.yml for development!

  4. you now need to use webpack-dev-server (in place of webpack or webpack-watcher).

  5. finally, enable React Hot Loader for your root components (the ones you register with WebpackerReact.setup):

    // For example in app/javascripts/components/hello.js
    import React from 'react'
    import { hot } from 'react-hot-loader'
    
    const Hello = () => <div>Hello World!</div>
    
    // This is the important line!
    export default hot(module)(Hello)
    

Development

To work on this gem locally, you first need to clone and setup the example application.

Then you need to change the example app Gemfile to point to your local repository and run bundle afterwise:

gem 'webpacker-react', path: '~/code/webpacker-react/'

Finally, you need to tell Yarn to use your local copy of the NPM module in this application, using yarn link:

$ cd ~/code/webpacker-react/javascript/webpacker_react-npm-module/
$ yarn
$ cd dist/
$ yarn             # compiles the code from src/ to dist/
$ yarn link
success Registered "webpacker-react".
info You can now run `yarn link "webpacker-react"` in the projects where you want to use this module and it will be used instead.
$ cd ~/code/webpacker-react-example/
$ yarn link webpacker-react
success Registered "webpacker-react".

After launching ./bin/webpack-watcher and ./bin/rails server in your example app directory, you can now change the Ruby or Javascript code in your local webpacker-react repository, and test it immediately using the example app.

Testing

If you changed the local javascript package, first ensure it is build (see above).

To run the test suite:

$ rake test

If you change the javascript code, please ensure there are no style errors before committing:

$ cd javascript/webpacker_react-npm-module/
$ yarn lint

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/renchap/webpacker-react. Please feel free to open issues about your needs and features you would like to be added.

Wishlist

  • [ ] server-side rendering (#3)

Thanks

This gem has been inspired by the awesome work on react-rails and react_on_rails. Many thanks to their authors!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].