Skip to content

Mapping Reactive Ruby to React.js

catmando edited this page Oct 27, 2015 · 1 revision

The basics:

Here is a react.js component using JSX

var CommentBox = React.createClass({
  render: function() {
    return (
      <div className="commentBox">
        Hello, world! I am a CommentBox.
      </div>
    );
  }
});

Here is the same component in ruby

class CommentBox
  include React::Component
  def render
    div(class: commentBox) { "Hello, world! I am a CommentBox." }
  end
end

Summary

Feature React.js Reactive-Ruby
create a component Use the React.createClass constructor any class that includes React::Component
the render method the render attribute a method named render
html elements use jsx (an augmented html embedded in javascript) all html tags are invoked as methods in components
tag attributes with some exceptions follows html syntax every tag takes an optional hash which is translated to the html tag attributes
class attribute must use className can use className, class or HAML style syntax (i.e. div.my_class)
Clone this wiki locally