No commit activity in last 3 years
No release in over 3 years
Component objects for Rails and React
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.14
>= 0
~> 10.0
~> 3.0

Runtime

 Project Readme

RailsReactComponents

Build Status

Component objects for Rails and React with react_on_rails

# component class
class MyComponent < RailsReactComponents::Component
  prop :name
  prop :email, on: :user
  prop :is_ready
  prop :received_email, as: :got_email

  def name
    "My name is Rails Component"
  end

  def is_ready
    user.ready?
  end

  def received_email
    false
  end
end

# in view this can run:
# react_component("MyComponent", 
#   props: { 
#     name: "My name is Rails Component",
#     email: "user@example.com",
#     isReady: true,
#     got_email: false
#   }
# )
render_react_component MyComponent, user: current_user 

TODO

  • - nested props
  • - Rspec matchers
  • - configuration (snake_case methods for camelCase props etc)

Installation

If you are not using react_on_rails yet check https://github.com/shakacode/react_on_rails

Add this line to your application's Gemfile:

gem 'rails_react_components'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rails_react_components

Configuration

You can add initializer (these are defaults):

  RailsReactComponents.config do |config|
    config.camelize_props = true # method :some_method will end up someMethod prop
                                 # when true, only way to prevent this is using prop as: option
    config.prerender = nil # set global prerender
    config.raise_on_prerender_error = nil # set global raise_on_prerender_error
  end

Usage

Available options example:

  class MyComponent < RailsReactComponents::Component
    component "FilePicker"

    id "file-picker-id"

    html_option "color", "red"
    html_option "opacity", "0.5"

    prerender
    trace
    replay_console
    raise_on_prerender_error

    prop :blank, include_blank: false

    prop :name, on: :user
    prop :email, delegate: :user # alias

    def blank
      ""
    end
  end

check also here and here

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Adam-Stomski/rails_react_components.

License

The gem is available as open source under the terms of the MIT License.