This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-02-05
Channels
- # announcements (17)
- # architecture (5)
- # babashka (12)
- # beginners (155)
- # calva (18)
- # chlorine-clover (2)
- # cider (57)
- # circleci (2)
- # clojure (151)
- # clojure-europe (4)
- # clojure-gamedev (20)
- # clojure-italy (18)
- # clojure-nl (4)
- # clojure-norway (3)
- # clojure-spec (8)
- # clojure-uk (95)
- # clojurescript (70)
- # core-async (68)
- # css (3)
- # data-science (13)
- # datascript (1)
- # datomic (16)
- # docker (2)
- # figwheel-main (41)
- # fulcro (34)
- # graalvm (6)
- # graphql (7)
- # jobs (14)
- # joker (2)
- # kaocha (1)
- # leiningen (2)
- # malli (3)
- # midje (2)
- # overtone (1)
- # reagent (8)
- # reitit (6)
- # ring-swagger (1)
- # schema (2)
- # shadow-cljs (6)
- # spacemacs (3)
- # specter (5)
- # timbre (3)
- # uncomplicate (1)
Hi, I'm trying to figure nesting components with local state. I have a form-2 component that fires a function that changes its local state when clicked.
(defn table-cell-simple "table cell component"
[content]
(let [clicked (reagent/atom false)]
(fn [] [:td {:style {:background (when @clicked "blue")} :on-click #(swap! clicked not)} "lorem ipsum"])))
This works all right, but when I try to render it like this:
(defn table-row "table row component" [row]
[:tr (doall (map-indexed #((table-cell-simple %2)) row))]
)
The color doesn't change, even though the function does get called, and the atom changes.
Maybe the row gets re-rendered and undo the change? or is it something else?
Any help wll be appreciatedTry with
(defn table-row [row]
[:tr (for [cell-value row]
[table-cell-simple cell-value]])
On a quick look, I think you're forgetting to pass content
into your render function - https://cljdoc.org/d/reagent/reagent/0.9.1/doc/frequently-asked-questions/why-isn-t-my-component-re-rendering-#props-change has some useful details
Note that my approach will issue a warning about a missing :key
. If you have such a key, just specify it. If not, you can just use
(defn table-row [row]
(into [:tr]
(map (fn [cell-value]
[table-cell-simple cell-value]))
row))
Thanks! @U2FRKM4TW it works now If I got it right, the problem was not using the reagent style square brackets around table-cell-simple, but for some reason reagent notation doesn't work with #() style? too much sugar maybe
With #()
, you created a new component each time something changed. With []
, the existing component was updated.
I think.
My work on getting Hooks working with Reagent is going on here: https://github.com/reagent-project/reagent/pull/477 It shows that there is some promise on getting Hooks working without completely breaking Reagent, but I don't have yet any idea what the performance is going to be with this approach.