Fork me on GitHub
#reagent
<
2023-01-01
>
Itay Dreyfus22:01:59

I have a hard time understanding how to use reagent with Clojure syntax properly… If this line

(def active? (get-in coffee-shops [:item-01 :level]))
which give me 5. However when calling it in another component, it renders nothing:
defn circle [items]
  [:div.level-list
   (for [item items]
     [:div.circle-normal {:style {:background-color
                                  (when (= item active?) "green")}} item])])

p-himik23:01:38

Does the JS console have any errors?

Itay Dreyfus06:01:36

just Warning: Each child in a list should have a unique “key” prop.

Itay Dreyfus06:01:58

don’t see something special

vanelsas07:01:01

You need to wrap the for call in a doall to force it to render. And between the for and div add a unique key. something like

^{:key item)}

Itay Dreyfus07:01:41

Like?

(doall (for [item items] 
            [:div.circle-normal {:style {:background-color
                                         (when (= item active?) "green")}} item]))])

vanelsas07:01:07

yes, that should render it

Itay Dreyfus07:01:44

If I change the item value to a number, it works, but if I leave active? it still not working

Itay Dreyfus07:01:28

And the REPL does prints me the correct value of “active?” :thinking_face:

Itay Dreyfus07:01:16

The circle component is being called here

(defn fancy-level-list []
  [:div.fancy-level-list
   [:p "Fancy list:"]
   [circle (range 1 11)]])

Itay Dreyfus07:01:30

Perhaps need to use a different fn than range?

vanelsas07:01:11

not sure what the circle component is

vanelsas07:01:26

what does it need as argument

Itay Dreyfus07:01:34

the above ^^

(defn circle [items active?]
  [:div.level-list
   [:div.new-title (map (comp :level val) coffee-shops)]
   (doall (for [item items] 
            [:div.circle-normal {:style {:background-color
                                         (when (= item active?) "green")}} item]))])

vanelsas07:01:25

You have the same issue with map btw

vanelsas07:01:53

map needs to be wrapped in a doall otherwise it won't do anything,since it is lazy

Itay Dreyfus07:01:21

Ohh yeah it’s just to render it on my page to see when it renders 😆

vanelsas07:01:41

In general, I would suggest to make the circle work for something simple first. Just to get it to render something. There are a bit too many possible issues for me to oversee it.

vanelsas07:01:22

I would try to render a string instead of an item first, if that appears you can take it a step further

vanelsas07:01:48

you also use when, wich means in the case the condition fails, it doesn't add a color to the item you are rendering

Itay Dreyfus07:01:58

Got it. Appreciate it, thanks! 🙂

vanelsas07:01:26

I don't see anything obvious wrong with your code. so syntax wise you are ok

✌️ 2
Itay Dreyfus13:01:10

That’s weird. I get to extract the level number in my main component, but when I want to implement it to work with the range fn, it doesn’t seems to work..

(defn circle [items]
  [:div.level-list
   (doall (for [item items] 
            [:div.circle-normal{:style {:background-color
                                        (map :level (vals coffee-shops) "green")}} item]))])
(defn fancy-level-list []
  [:div.fancy-level-list
   [:p "Fancy list:"]
   [circle (range 1 11)]])
(defn item-card []
  [:div.items-card-list
   (for [item (vals coffee-shops)]
     [:div.item-card
      [:div.item-header
       [:div.icon-title
        [:div.icon-img
         [:img.item-icon {:src (:img item)}]]
        [:div.title-desc
         [:p.item-title (:title item)] 
         [:p.item-desc (:description item)]
         [:p.item-location (:location item)]
         [:p (:level item)]]]
       [:div.badge-ig
        [:a {:href ""} "IG"]
        [badge]]]
      [fancy-level-list]
      ])])

p-himik13:01:49

If you create a public MRE, I can take a look.

Itay Dreyfus13:01:15

like a github repo?

p-himik13:01:42

With proper instructions on how to run it.

Itay Dreyfus14:01:20

the file is items_list.cljs in the components folder

p-himik14:01:06

FYI the top 5 dirs that GitHub UI shows should all be in .gitignore. Looking at the code now...

👍 2
p-himik14:01:37

Also, seems like you have main set as the default branch, but it's almost empty and the actual code is in the master branch. But no biggie here.

Itay Dreyfus14:01:01

yes yes, I’m quite new to this, so still learning 😉

👍 2
p-himik14:01:18

This code is definitely wrong:

{:background-color (map :level (vals coffee-shops) "green")}
You provide a lazy collection as a color. More than that - you're using map in a way that you probably don't want to be using here. I'm not sure exactly what you want to achieve here. Can you elaborate?

Itay Dreyfus14:01:33

Basically I want to change the background color of the circle component based on the level value in data.cljs

Itay Dreyfus14:01:17

So I though to extract the respective numbers and then manipulate them which is probably not a solution

p-himik15:01:10

But your range of items has numbers from 1 to 10, and all the levels in the data NS have only 5, 3, and 8. And even those few are strings and not numbers.

p-himik15:01:34

Maybe that's exactly the problem that you were facing at the very start, with active? seemingly not working - because in CLJS, as opposed to JS, (= 1 "1") is actually false.

Itay Dreyfus15:01:15

Yep, the level values are strings and not numbers 😑

Itay Dreyfus15:01:31

It seems to be working now!

🎉 2
Itay Dreyfus17:01:18

Thanks a ton!

👍 2