Fork me on GitHub

I am creating a reagent form and trying to update an atom which is a map. The value I want to update is itself a map so I want to do (reset! com-form update-in [:new-price :quantity] (-> evt .-target .-value)) . The threading macro is confusing me but there must be a way to do this.


@dfcarpenter you want swap! not reset!


your threading macro is correct (also if it ever confuses you, run the form through macroexpand)


(def com-form (r/atom {:name              ""
                       :desc              ""
                       :new-price        nil
                       :prices            []
                       :assets            []}))

(defn new-component []
  (let [comform @com-form]
        [:input.input {:type "text"
                        :name "name"
                        :placeholder "name"
                        :value (get comform :name)
                        :on-change #(swap! com-form update :name (-> % .-target .-value))}]
        [:button {:on-click #(swap! com-form assoc :new-price {:pricingType ""
                                                               :currency    ""
                                                               :costValue   0
                                                               :quantity    0})} "Add Price"]
I keep getting tons of errors, specifically
Uncaught TypeError: is not a function
    at core.cljs:5283
    at Function.cljs$core$IFn$_invoke$arity$3 (core.cljs:5283)
    at Object.cljs$core$ISwap$_swap_BANG_$arity$4 (ratom.cljs:146)
    at Function.cljs$core$IFn$_invoke$arity$4 (core.cljs:856)
    at Function.cljs$core$IFn$_invoke$arity$4 (core.cljs:4470)
    at eval (views.cljs:63)
    at HTMLUnknownElement.callCallback (react-dom.development.js:101)
    at Object.invokeGuardedCallback (react-dom.development.js:139)
    at Object.invokeGuardedCallback (react-dom.development.js:188)
    at Object.invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:202)


you want assoc instead of update


look at the docs for update--its third argument is a function not a value


if you run (swap! (reagent/atom {:foo 1}) update :foo 2) in a cljs repl you’ll get that is not a function error