This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-09-15
Channels
- # ai (35)
- # announcements (3)
- # babashka (16)
- # babashka-sci-dev (2)
- # beginners (37)
- # biff (16)
- # calva (5)
- # cider (2)
- # clj-commons (81)
- # clj-kondo (29)
- # cljfx (2)
- # cljs-dev (4)
- # clojars (4)
- # clojure (92)
- # clojure-europe (72)
- # clojure-losangeles (8)
- # clojure-nl (1)
- # clojure-norway (10)
- # clojure-uk (1)
- # clojurescript (20)
- # clojutre (2)
- # conjure (2)
- # data-science (18)
- # datomic (1)
- # emacs (10)
- # fulcro (49)
- # joyride (1)
- # kaocha (23)
- # leiningen (8)
- # lsp (14)
- # meander (5)
- # off-topic (93)
- # polylith (4)
- # re-frame (20)
- # reagent (9)
- # reitit (2)
- # remote-jobs (8)
- # sci (1)
- # shadow-cljs (21)
- # testing (3)
- # vim (27)
- # xtdb (35)
How do you trigger a Reagent element to rerender if the atom its state depends on has changed, but it doesn't rerender?
(let [completed (r/atom false)
...
[:form {:on-submit (fn [e] (.preventDefault e) (reset! completed false))}
...
[input {:type "checkbox" :value @completed}]])
If I pprint @completed
I can see that completed
toggles between true and false, but the checkbox remains checked.IIRC, <input type="checkbox">
doesn't have a value but rather has a checked
property.
Yeah, you can see an example here: https://reactjs.org/docs/forms.html#handling-multiple-inputs
@U2FRKM4TW If you've got the time 🙂
Warning: Functions are not valid as a React child.
How do you map over a list of maps to generate components?
(defn todo-list []
[:ul (map todo-item @todos)])
Tried various combinations of mapv, doall, doseq, for.
Passing one map at a time works fine, todo-item
works and todos
is populated.Are you sure that warning comes from that very component and not from some other place?
(defn todo-item [{:keys [desc completed]}]
(let [completed (r/atom completed)]
(fn []
[:li {:style {:color (if @completed "green" "red")}}
[:p desc]
[:button {:on-click #(reset! completed (not @completed))}
"Toggle done"]])))
I thought that the fn []
, but adding in a parameter changes nothing.
What works is:
[todo-item (first @todos)]
What doesn't work is:
[:ul (map todo-item @todos)]
You're using todo-item
as a function. But you gotta use it as a component.
Here's how I'd use it:
(into [:ul]
(map (fn [todo]
[todo-item todo])
@todos)
Also, unrelated but don't use reset!
with @
. Instead, use swap!
: #(swap! completed not)
.Cheers!
(for [todo @todos] [todo-item todo)
Thanks for the swap!
suggestion, much cleaner.Note that your for
code will produce a warning about missing :key
. So if every todo item has a unique and stable ID of some kind, use it as a key. If not, you can use into
to avoid that warning.
Why does swap!
turn the map into nil here? Calling without swap yields the correct output:
{1 {:flag false}, 2 {:flag true}}
Because the first argument to swap!
is a function. But you're passing the result of (update-in ...)
instead.
Man, you're the hero of my day. Thank you, once again.
@U2FRKM4TW I've got Calva up and running now, hopefully I'll be on my own two legs by now, cheers 😂
if I use swap!
like so (swap! winner calculate-winner state)
where state
is another r/atom
must I deref state
when I pass it as an arg?