reagent

Itay Dreyfus 2023-01-01T22:59:59.314229Z

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-himik 2023-01-01T23:52:38.379859Z

Does the JS console have any errors?

Itay Dreyfus 2023-01-02T06:21:36.562999Z

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

Itay Dreyfus 2023-01-02T06:21:58.066609Z

don’t see something special

Itay Dreyfus 2023-01-02T06:26:47.688419Z

vanelsas 2023-01-02T07:35:01.170529Z

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 Dreyfus 2023-01-02T07:39:41.774689Z

Like?

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

vanelsas 2023-01-02T07:40:07.183979Z

yes, that should render it

Itay Dreyfus 2023-01-02T07:40:44.468489Z

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

Itay Dreyfus 2023-01-02T07:41:28.412149Z

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

Itay Dreyfus 2023-01-02T07:42:16.792849Z

The circle component is being called here

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

Itay Dreyfus 2023-01-02T07:42:30.417629Z

Perhaps need to use a different fn than range?

vanelsas 2023-01-02T07:43:11.725149Z

not sure what the circle component is

vanelsas 2023-01-02T07:43:26.521279Z

what does it need as argument

Itay Dreyfus 2023-01-02T07:43:34.901609Z

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]))])

vanelsas 2023-01-02T07:44:25.154769Z

You have the same issue with map btw

vanelsas 2023-01-02T07:44:53.982319Z

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

Itay Dreyfus 2023-01-02T07:45:21.370539Z

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

vanelsas 2023-01-02T07:46:41.843749Z

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.

vanelsas 2023-01-02T07:47:22.500049Z

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

vanelsas 2023-01-02T07:47:48.430749Z

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 Dreyfus 2023-01-02T07:47:58.646749Z

Got it. Appreciate it, thanks! 🙂

vanelsas 2023-01-02T07:48:26.232959Z

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

✌️ 1
Itay Dreyfus 2023-01-03T13:45:10.617339Z

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-himik 2023-01-03T13:55:49.376529Z

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

Itay Dreyfus 2023-01-03T13:57:15.045099Z

like a github repo?

p-himik 2023-01-03T13:57:32.395999Z

Yep.

Itay Dreyfus 2023-01-03T13:57:40.642279Z

awesome

p-himik 2023-01-03T13:57:42.521039Z

With proper instructions on how to run it.

Itay Dreyfus 2023-01-03T14:10:50.737239Z

Uploaded here https://github.com/itaydre/coffee-tlv/tree/master

Itay Dreyfus 2023-01-03T14:12:20.887329Z

the file is items_list.cljs in the components folder

p-himik 2023-01-03T14:17:06.112269Z

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

👍 1
p-himik 2023-01-03T14:18:37.374149Z

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 Dreyfus 2023-01-03T14:19:01.663519Z

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

👍 1
p-himik 2023-01-03T14:23:18.316249Z

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 Dreyfus 2023-01-03T14:24:33.931909Z

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

Itay Dreyfus 2023-01-03T14:25:17.132759Z

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

p-himik 2023-01-03T15:05:10.636749Z

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-himik 2023-01-03T15:07:34.927959Z

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 Dreyfus 2023-01-03T15:08:15.497899Z

Yep, the level values are strings and not numbers 😑

Itay Dreyfus 2023-01-03T15:09:31.453919Z

It seems to be working now!

🎉 1
Itay Dreyfus 2023-01-03T17:56:18.268389Z

Thanks a ton!

👍 1