Skip to content

Instantly share code, notes, and snippets.

@esciafardini
Last active May 2, 2021
Embed
What would you like to do?
having an issue with bi-directional data binding
(def temps
(r/atom {:c 0 :f 32}))
----------------------------------
(defn c-to-f
[temp]
(+ (* temp 1.8) 32))
(defn f-to-c
[temp]
(* (- temp 32) .555555555555))
(defn celcius-input [value]
[:input {:type "text"
:value value
:on-change #(assoc state/temps :c (-> % .-target .-value) :f (c-to-f (-> % .target .-value)))}])
(defn celcius-display []
(let [val (get @state/temps :c)]
(fn []
[:div
[:p "The Celcius Temperature is: " [celcius-input val]]])))
(defn fahrenheit-input [value]
[:input {:type "text"
:value value
:on-change #(assoc state/temps :f (-> % .-target .-value) :c (f-to-c (-> % .target .-value)))}])
(defn fahrenheit-display []
(let [val (get @state/temps :f)]
(fn []
[:div
[:p "The Celcius Temperature is: " [fahrenheit-input val]]])))
(defn gui-two []
[:span.main
[fahrenheit-display]
[celcius-display]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment