Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

Commit 82cf81b

Browse files
committed
README draft
1 parent be88197 commit 82cf81b

File tree

1 file changed

+126
-11
lines changed

1 file changed

+126
-11
lines changed

README.md

+126-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,133 @@
1-
# react.rb
1+
# React.rb
22

3-
The idea is
3+
**React.rb is a [Opal Ruby](http://opalrb.org) wrapper of [React.js library](http://facebook.github.io/react/)**.
44

5-
1. Take advantage of React.js 's Virtual DOM
6-
2. Establish a higher level of helpers's (rather than write low level DOM)
7-
3. Make Ruby / Rails developers feel like home
5+
It let you write reactive UI component with Ruby's elegancy and compiled to run in Javascript.
86

9-
## Developing
7+
## Installation
8+
9+
```ruby
10+
gem `react.rb`
11+
```
12+
13+
and in your Opal application,
14+
15+
```ruby
16+
require "opal"
17+
require "react"
18+
React.render(React.create_element('h1'){ "Hello World!" }, `document.body`)
19+
```
20+
21+
For integration with server (Sinatra, etc), see setup of [TodoMVC](example/todos) or the [official docs](http://opalrb.org/docs/) of Opal.
22+
23+
## Usage
24+
25+
### A Simple Component
26+
27+
A ruby class which define method `render` is a valid component.
28+
29+
```ruby
30+
class HelloMessage
31+
def render
32+
React.create_element("div") { "Hello World!" }
33+
end
34+
end
35+
36+
React.render_static_markup(React.create_element(HelloMessage)) # => '<div>Hello World!</div>'
37+
```
38+
39+
### More complicated one
40+
41+
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 with using the corresponding snake case method name.
42+
43+
```ruby
44+
class HelloMessage
45+
def initialize(native)
46+
@native = Native(native)
47+
end
48+
49+
def component_will_mount
50+
puts "will mount!"
51+
end
52+
53+
def render
54+
React.create_element("div") { "Hello #{@native[:props][:name]}!" }
55+
end
56+
end
57+
58+
puts React.render_static_markup(React.create_element(HelloMessage, name: 'John'))
59+
60+
# => will_mount!
61+
# => '<div>Hello John!</div>'
62+
```
63+
64+
### React::Component
65+
66+
Hey, we are using Ruby, simply include `React::Component` to save your typing and have some handy method defined.
1067

11-
1. `bundle install`
12-
2. `bundle exec rackup`
13-
3. Open `http://localhost:9292` to run the test
68+
```ruby
69+
class HelloMessage
70+
define_state(:foo) { "Default greeting" }
71+
72+
before_mount do
73+
self.foo = self.foo + " <3 "
74+
end
75+
76+
def render
77+
div do
78+
span { self.foo + " Hello #{params[:name]}!" }
79+
end
80+
end
81+
end
82+
83+
React.render_static_markup(React.create_element(HelloMessage, name: 'John')) # => '<div>Hello John!</div>'
84+
```
85+
86+
* Callback of life cycle could be created through `before_mount`, `after_mount`, etc
87+
* `this.props` is provided through method `self.params`
88+
* Use class method `define_method` to create setter & getter of `this.state` for you
89+
90+
### Props validation
91+
92+
How about props validation? Inspired from [Grape API](https://github.com/intridea/grape), props validation rule could be create easily through `params` class method as below,
93+
94+
```ruby
95+
class App
96+
params do
97+
requires :username, type: String
98+
requires :enum, values: ['foo', 'bar', 'awesome']
99+
requires :payload, type: Todo # yeah, a plain Ruby class
100+
optional :filters, type: Array[String]
101+
optional :flash_message, type: String, default: 'Welcome!' # no need to feed through `getDefaultProps`
102+
end
103+
104+
def render; end
105+
end
106+
```
14107

15108
## Example
16109

17-
* React Tutorial: see [example/react-tutorial](example/react-tutorial/README.md)
18-
* TodoMVC: see [example/todos](example/todos/README.md)
110+
* React Tutorial: see [example/react-tutorial](example/react-tutorial), the original CommentBox example.
111+
* TodoMVC: see [example/todos](example/todos), your beloved TodoMVC <3.
112+
113+
## Developing
114+
115+
To run the test case of the project yourself.
116+
117+
1. `git clone` the project
118+
2. `bundle install`
119+
3. `bundle exec rackup`
120+
4. Open `http://localhost:9292` to run the spec
121+
122+
## Contributions
123+
124+
This project is still in early stage, so discussion, bug report and PR are really welcome :wink.
125+
126+
## Contact
127+
128+
[David Chang](http://github.com/zetachang)
129+
[@zetachang](https://twitter.com/zetachang)
130+
131+
## License
132+
133+
In short, IPSqueezableViewController is available under the MIT license. See the LICENSE file for more info.

0 commit comments

Comments
 (0)