
Research
Security News
Lazarus Strikes npm Again with New Wave of Malicious Packages
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
React.rb is an Opal Ruby wrapper of React.js library.
It lets you write reactive UI components, with Ruby's elegance and compiled to run in JavaScript. :heart:
# Gemfile
gem "react.rb"
and in your Opal application,
require "opal"
require "react"
React.render(React.create_element('h1'){ "Hello World!" }, `document.body`)
For integration with server (Sinatra, etc), see setup of TodoMVC or the official docs of Opal.
A ruby class which define method render
is a valid component.
class HelloMessage
def render
React.create_element("div") { "Hello World!" }
end
end
puts React.render_to_static_markup(React.create_element(HelloMessage))
# => '<div>Hello World!</div>'
To hook into native ReactComponent life cycle, the native this
will be passed to the class's initializer. And all corresponding life cycle methods (componentDidMount
, etc) will be invoked on the instance using the snake-case method name.
class HelloMessage
def initialize(native)
@native = Native(native)
end
def component_will_mount
puts "will mount!"
end
def render
React.create_element("div") { "Hello #{@native[:props][:name]}!" }
end
end
puts React.render_to_static_markup(React.create_element(HelloMessage, name: 'John'))
# => will_mount!
# => '<div>Hello John!</div>'
Hey, we are using Ruby, simply include React::Component
to save your typing and have some handy methods defined.
class HelloMessage
include React::Component
MSG = {great: 'Cool!', bad: 'Cheer up!'}
define_state(:foo) { "Default greeting" }
before_mount do
self.foo = "#{self.foo}: #{MSG[params[:mood]]}"
end
after_mount :log
def log
puts "mounted!"
end
def render
div do
span { self.foo + " #{params[:name]}!" }
end
end
end
class App
include React::Component
def render
present HelloMessage, name: 'John', mood: 'great'
end
end
puts React.render_to_static_markup(React.create_element(App))
# => '<div><span>Default greeting: Cool! John!</span></div>'
React.render(React.create_element(App), `document.body`)
# mounted!
before_mount
, after_mount
, etcthis.props
is accessed through method self.params
define_state
to create setter & getter of this.state
for youAs a replacement of JSX, include React::Component
and you can build React.Element
hierarchy without all the React.create_element
noises.
def render
div do
h1 { "Title" }
h2 { "subtitle"}
div(class_name: 'fancy', id: 'foo') { span { "some text #{interpolation}"} }
present FancyElement, fancy_props: '10'
end
end
Not a fan of using element building DSL? Use file extension .jsx.rb
to get JSX fragment compiled.
# app.jsx.rb
class Fancy
def render
`<div>"this is fancy"</div>`
end
end
class App
include React::Component
def render
element = %x{
<div>
<h1>Outer</h1>
<Fancy>{ #{5.times.to_a.join(",")} }</Fancy>
</div>
}
element
end
end
React.expose_native_class(Fancy)
React.render React.create_element(App), `document.body`
How about props validation? Inspired by Grape API, props validation rule could be created easily through params
class method as below,
class App
include React::Component
params do
requires :username, type: String
requires :enum, values: ['foo', 'bar', 'awesome']
requires :payload, type: Todo # yeah, a plain Ruby class
optional :filters, type: Array[String]
optional :flash_message, type: String, default: 'Welcome!' # no need to feed through `getDefaultProps`
end
def render
div
end
end
Simply create a Ruby module to encapsulate the behavior. Example below is modified from the original React.js Exmaple on Mixin. Opal Browser syntax are used here to make it cleaner.
module SetInterval
def self.included(base)
base.class_eval do
before_mount { @interval = [] }
before_unmount do
# abort associated timer of a component right before unmount
@interval.each { |i| i.abort }
end
end
end
def set_interval(seconds, &block)
@interval << $window.every(seconds, &block)
end
end
class TickTock
include React::Component
include SetInterval
define_state(:seconds) { 0 }
before_mount do
set_interval(1) { self.seconds = self.seconds + 1 }
set_interval(1) { puts "Tick!" }
end
def render
span do
"React has been running for: #{self.seconds}"
end
end
end
React.render(React.create_element(TickTock), $document.body.to_n)
$window.after(5) do
React.unmount_component_at_node($document.body.to_n)
end
# => Tick!
# => ... for 5 times then stop ticking after 5 seconds
For React Native support, please refer to Opal Native.
To run the test case of the project yourself.
git clone
the projectbundle install
bundle exec rackup
http://localhost:9292
to run the specThis project is still in early stage, so discussion, bug report and PR are really welcome :wink:.
In short, React.rb is available under the MIT license. See the LICENSE file for more info.
FAQs
Unknown package
We found that react.rb demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.
Security News
Opengrep continues building momentum with the alpha release of its Playground tool, demonstrating the project's rapid evolution just two months after its initial launch.