Fork me on GitHub
#reagent
<
2018-09-17
>
dfcarpenter20:09:11

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.

justinlee20:09:39

@dfcarpenter you want swap! not reset!

justinlee20:09:13

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

dfcarpenter21:09:47

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

(defn new-component []
  (let [comform @com-form]
    [:div
      [:div
        [:div
        [: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: f.call 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)


justinlee21:09:53

you want assoc instead of update

justinlee21:09:07

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

justinlee21:09:55

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