npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-components-rails

v1.0.0-beta.4

Published

_**Note:** This project was formerly known as `webpacker-rails`. Following Webpacker's deprecation, it has been renamed and rewritten to no longer rely on Webpacker. Documentation for the latest `webpacker-rails` release (1.0.0-beta.1) is [available here]

Downloads

285

Readme

React-Components-Rails

Note: This project was formerly known as webpacker-rails. Following Webpacker's deprecation, it has been renamed and rewritten to no longer rely on Webpacker. Documentation for the latest webpacker-rails release (1.0.0-beta.1) is available here.

React-Components-Rails makes it easy to use React with your Rails applications. It provides Controller and View helpers to render React Components on your application, and does not case about the way you ship your Javascript

Installation

First, you need to add this gem to your Rails app Gemfile:

gem 'react-components-rails', "~> 1.0.0.beta.4"

Once done, run bundle to install the gem.

Then you need to update your package.json file to include the react-components-rails Javascript module:

yarn add react-components-rails

You are now all set!

Note about versions

React-Components-Rails 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 app entry file, import your components as well as react-components-rails and register them. Considering you have a component in app/javascript/components/hello.js:

import Hello from "components/hello"
import ReactComponentsRails from "react-components-rails"

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

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.

Hot Module Replacement

It should be supported out of the box, if supported by your Javascript stack. Please refer to your Javascript compiler/bundler documentation to do so.

React versions

This package tries to support both the legacy React DOM interface (ReactDOM.render) and the new one introduced in React 18 (ReactDOM.createRoot).

The installed React version is tested at runtime by trying to import react-dom/client. If the import succeeds then the new API is used, otherwise we fallback to the legacy API.

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.

Thanks

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