Skip to content

Instantly share code, notes, and snippets.

@haywoood
Created December 19, 2013 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haywoood/8044484 to your computer and use it in GitHub Desktop.
Save haywoood/8044484 to your computer and use it in GitHub Desktop.
Om translation of simple react.js
(ns om-todos.core
(:require [om.core :as om :include-macros true]
[om.dom :as dom :include-macros true]))
;var Timer = React.createClass({
; getInitialState: function() {
; return {secondsElapsed: 0};
; },
; tick: function() {
; this.setState({secondsElapsed: this.state.secondsElapsed + 1});
; },
; componentDidMount: function() {
; this.interval = setInterval(this.tick, 1000);
; },
; componentWillUnmount: function() {
; clearInterval(this.interval);
; },
; render: function() {
; return React.DOM.div({},
; 'Seconds Elapsed: ', this.state.secondsElapsed
; );
; }
;});
(def app-state (atom {:secondsElapsed 1}))
(defn inc-property [app prop]
(om/update! app [prop]
(fn [prop] (inc prop))))
(defn timer [{:keys [secondsElapsed] :as app}]
(reify
om/IDidMount
(did-mount [_ owner _]
(om/set-state! owner [:interval]
(js/setInterval #(inc-property app :secondsElapsed) 1000)))
om/IWillUnmount
(will-unmount [_ owner]
(js/clearInterval (om/get-state owner [:interval])))
om/IRender
(render [this owner]
(dom/div nil
(str "Seconds Elapsed: "
secondsElapsed)
(dom/div nil
(dom/button
#js {:onClick #(js/clearInterval (om/get-state owner [:interval]))}
"Stop"))))))
(om/root app-state timer js/document.body)
@przeor
Copy link

przeor commented Aug 29, 2016

https://reactjs.co This is the official free online convention and tutorial book for React.JS Developers. React is not only the View (in MVC) anymore. ReactJS For Dummies: Why & How to Learn React Redux, the Right Way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment