This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-10-05
Channels
- # announcements (14)
- # aws (7)
- # babashka (28)
- # beginners (16)
- # calva (2)
- # cider (1)
- # clj-commons (8)
- # clj-kondo (29)
- # clojure (213)
- # clojure-europe (39)
- # clojure-losangeles (2)
- # clojure-norway (9)
- # clojure-spec (2)
- # clojurescript (11)
- # community-development (1)
- # conjure (2)
- # cursive (6)
- # datalevin (2)
- # datomic (8)
- # emacs (29)
- # events (1)
- # fulcro (22)
- # graalvm (14)
- # improve-getting-started (1)
- # jobs (1)
- # lambdaisland (5)
- # leiningen (4)
- # lsp (7)
- # malli (13)
- # meander (11)
- # membrane (13)
- # off-topic (23)
- # polylith (9)
- # re-frame (4)
- # reagent (7)
- # reitit (6)
- # releases (2)
- # sql (58)
- # testing (8)
- # tools-deps (18)
- # web-security (2)
I have a form inside a modal that I am using to send data to my backend. I was having issues with getting the form to update state on the component but I just resolved these. But when I click the button to send the data to the server, it sends back a successful response with the atoms I just updated as nil
. This is what the component looks like:
(defn modal []
(let [title (rc/atom nil)
desc (rc/atom nil)
add-book (fn [] (p/let [_resp (js/fetch "/books/" (clj->js {:method "POST"
:body {:title @title :description @desc :status true}
:headers {:Content-Type "application/json"}}))
data (.json _resp)]
(println data)))]
[:div {:class "modal", :id "book-modal" :tab-index "-1", :role "dialog"}
[:div {:class "modal-dialog", :role "document"}
[:div {:class "modal-content"}
[:div {:class "modal-header"}
[:h5 {:class "modal-title"} "Add a book"]
[:button {:type "button", :class "close", :data-dismiss "modal", :aria-label "Close"}
[:span {:aria-hidden "true"} "×"]]]
[:div {:class "modal-body"}
[:div.d-flex.justify-content-center.text-center
[:div.form-group
[:div.form-label.text-white "Book"]
[:input.form-control.m-2 {:type "text"
:placeholder "Enter book title"
:on-change (fn [e] (reset! title (.. e -target -value)))}]
[:input.form-control.m-2 {:type "text"
:placeholder "Book description"
:on-change (fn [e] (reset! desc (.. e -target -value)))}]
[:button.m-2 {:on-click #(add-book)} "Add book"]]]]
[:div {:class "modal-footer"}
[:button {:type "button", :class "btn btn-primary"} "Save changes"]
[:button {:type "button", :class "btn btn-secondary", :data-dismiss "modal"} "Close"]]]]]))
You mean, that (println data)
prints something like {:title nil, :description nil, :status true}
?
It's a #js
map that also includes an _id
field from Mongo
Well, that's a backend issue then. Whatever your server sends, your client receives and has no control over.
Ok, I thought as much, I just wanted to be sure. I'm still shedding the burden of thinking about things explicitly as "props" and "state" and thought I might be doing something silly there. Thank you.
Note also that it would be better to use a <form>
tag with {:on-submit (fn [^js e] (.preventDefault e))}
, and a :type :submit
button (probably that "Add book").
This way, hitting Enter in any of the fields would just work and there's potential of autofilling software, like password and note managers, working better with such a form.