Professional Documents
Culture Documents
Om and Clojurescript
Om and Clojurescript
Om and Clojurescript
Om and React
LambdaJam - Brisbane, 2014
Leonardo Borges
@leonardo_borges
www.leonardoborges.com
www.thoughtworks.com
About
ThoughtWorker
Functional Programming & Clojure
advocate
Founder of the Sydney Clojure User
Group
Functional Programming
is on the rise
Because Javascript
[1,3,5].map(parseInt);!
// [1, NaN, NaN]!
React
Created by Facebook for building user interfaces
The V in MVC
Self-contained components
Doesnt make assumptions about your stack - can be used with anything
Self-contained components
React combines display logic and DOM generation
Components are themselves loosely coupled
The whole app is re-rendered on every update
Virtual DOM
Efficient diff algorithm
React.renderComponent(!
HelloMessage( {name:"John"} ), mountNode);
No literals?
React.renderComponent(!
<HelloMessage name="John" />, mountNode);
A simple Om component
(def app-state (atom {:name "Leo"}))!
!
IWillMount
IShouldUpdate
IRender
IShouldUpdate
Called on app state changes but before rendering
This is where React uses its fast diff algorithm
Om components implement the fastest algorithm possible: a simple
reference equality check
Generally, you wont have to implement this
IRender
Same as IRenderState
except it doesnt depend on the component local state to render
IRender
(def app-state (atom {:name "Leo"}))!
!
A larger example
A larger example
Implementing undo
(def app-state
(atom speaker-data))!
(def app-history (atom [@app-state]))!
!
Implementing reset
(defn reset-app-state []!
(reset! app-state (first @app-history))!
(reset! app-history [@app-state]))!
Summary
With Om, youre not using a crippled template language, you can
leverage all of Clojurescript (including other DOM libraries)
Rendering and display logic are inevitably coupled. Om/React
acknowledges that a bundles them in components
The whole app is re-rendered on every state change, making it easier to
reason about
This is efficient thanks to immutable data structures
Summary
Clojurescript also provides a better development experience with a
powerful browser REPL much like what youd get with Clojure on the JVM
Source maps are here today
Bottom line is that Clojurescript is a serious contender
References
Code: https://github.com/leonardoborges/lambdajam-2014-om-talk
React documentation: http://facebook.github.io/react/
Om documentation: https://github.com/swannodette/om/wiki/
Documentation#build
Basic Tutorial: https://github.com/swannodette/om/wiki/Basic-Tutorial
Thanks!
Questions?
Leonardo Borges
@leonardo_borges
www.leonardoborges.com
www.thoughtworks.com