opalrbreact.rb

opal react.rb passing in proc as a param for use as a callback


I can't seem to recall about passing in a proc as a param for a component in react.rb, can someone remind me?

Sorry if my question is badly worded, I'm a little tired.


Solution

  • Sure checkout http://reactrb.org/docs/reusable-components.html (about half way down the page) or http://reactrb.org/docs/component-api.html

    Here is a working example

    <div id="container"></div>
    <script type="text/ruby">
    class Alarm < React::Component::Base
      param :at, type: Time
      param :notify, type: Proc
      after_mount do
        @clock = every(1) do
          if Time.now > params.at
            params.notify
            @clock.stop
          end
          force_update!
        end
      end
      def render
        "#{Time.now}"
      end
    end
    
    Element['#container'].render do
      Alarm(
       at: Time.now+10.seconds, 
       notify: -> { alert ('beep beep beep') }
      )
    end
    
    </script>
    
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <script src="https://rawgit.com/reactive-ruby/inline-reactive-ruby/master/inline-reactive-ruby.js"></script>