require 'react' class Timer < React::Component def initial_state { seconds_elapsed: 0 } end def tick set_state(seconds_elapsed: state[:seconds_elapsed] + 1) end def component_did_mount @interval = window.setInterval(-> { tick }, 1000) end def component_will_unmount window.clearInterval(@interval) end def render div("Seconds Elapsed: #{state[:seconds_elapsed]}") end end Timer.run