Skip to content

Instantly share code, notes, and snippets.

@igrishaev
Last active October 7, 2018 13:22
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 igrishaev/83b6c8f5922b9ff6015cff99d69be72a to your computer and use it in GitHub Desktop.
Save igrishaev/83b6c8f5922b9ff6015cff99d69be72a to your computer and use it in GitHub Desktop.
Managing state in Reagent components
(def React js/React)
(def ReactDOM js/ReactDOM)
(def create-element React.createElement)
#_
(defn item
[{:keys [value label]}]
(create-element
ReactNative.Picker.Item
#js {:label label :value value}))
(defn AAA
[props & args]
(this-as this
(create-element
"div" #js {}
(create-element
"input" #js {:value (or (some-> this .-state .-value)
"")
:onChange
(fn [e]
(let [value (some-> e .-target .-value)]
(.setState this #js {:value value})))}))))
;; context updater
(defn MyComp
[props]
(let [this (js-this)]
(js/React.Component.call this props)
(set! (.-state this) #js {:value "ssss"})
this)
#_
(this-as this
(js/React.Component.call this props)
(set! (.-state this) #js {:value "ssss"})
))
(js/goog.object.extend
(.-prototype MyComp)
js/React.Component.prototype
#js {
:render
(fn []
(let [foo (atom nil)]
(this-as this
;; (print this)
;; (js/console.log this)
(create-element
"div" nil
(create-element
"input" #js {:value (or
(some-> this .-state .-value :foo)
""
)
:onChange
(fn [e]
(some-> this .-state js/console.log)
(let [value (some-> e .-target .-value)]
(reset! foo value)
;; (set! (.-state this) {:value value})
(.setState this #js {:value {:foo value}})
))})))))})
(defn ^:export init
[]
(.render ReactDOM
(create-element MyComp #js {:value "ssss"})
(js/document.getElementById "page"))
(def ReactNative (js/require "react-native"))
(def React (js/require "react"))
(def create-element React.createElement)
(defn MyPicker
[& args]
(this-as this
(apply ReactNative.Picker this args)))
(defn item
[{:keys [value label]}]
(create-element
ReactNative.Picker.Item
#js {:label label :value value}))
(js/goog.object.extend
(.-prototype MyPicker)
(.-prototype ReactNative.Picker)
#js {:render
(fn []
(this-as this
(create-element
ReactNative.Picker
#js {:selectedValue
(some-> this .-state .-selectedValue)
:onValueChange
(fn [value index]
(.setState this #js {:selectedValue value}))}
(to-array (map item assets/countries)))))})
(def my-picker
(r/adapt-react-class MyPicker))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment