在 Rails 应用程序中添加一个简单的反应组件(用 ruby 编写)
-
将 hyperloop gem 添加到 rails(4.0 - 5.1)Gemfile 中
-
bundle install
-
将 hyperloop 清单添加到 application.js 文件中:
// app/assets/javascripts/application.js ... //= hyperloop-loader
-
创建你的 react 组件,并将它们放在
hyperloop/components
目录中# app/hyperloop/components/hello_world.rb class HelloWorld < Hyperloop::Component after_mount do every(1.second) { mutate.current_time(Time.now) } end render do "Hello World! The time is now: #{state.current_time}" end end
-
组件就像视图一样。它们在控制器中使用
render_component
方法挂载:# somewhere in a controller: ... def hello_world render_component # renders HelloWorld based on method name end