Fork me on GitHub
JB Seo00:10:05

Reagent's reagent.core/children does not support functional component now. As reagent.core/current-component returns mocking object, I write a helper function like below:

(defn get-children [^js this]
  (if (.-props this)
    (reagent.core/children this)
    (when-some [v (.-argv this)]
      (reagent.impl.component/extract-children v))))
Can I safely use it for functional component? for example
(defmacro nest-children []
  `(some->> (reagent.core/current-component)
            (into [:<>])))

(defn fn-comp [{:keys [prop1]}]
   (str "Prop1 is " prop1)

(defn demo []
  [:f> fn-comp
   {:prop1 "hello"}
   [:span "world"]
   [:span "foo"]
   [:span "bar"]])
It seems work for simple tests.


You might get better answers in #reagent But also, I wouldn't use a macro for that, it's an unnecessary complication.

JB Seo13:10:07

Thanks. I want to avoid macro, too. however, nest-children doesn't work when I turn it to a function.


Did you remove that backtick upon turning it into a function?


sorry for asking so much but why does my degree tag get not updated when I choose a output type. Code so far :


oke this seems to work

(defn update-output [_]
  (gdom/setTextContent degree get-output-temp))
but now the new degree is added to degree here instead of changing things


<h3 id="convertedUnit">F</h3>


I changed also this : (def degree (gdom/getElement "convertedUnit"))


Never mind , got it working with

(defn update-output [_]
    (gdom/setTextContent degree (get-output-temp))) 


its just "change" there not "on-change"


thanks, I changed it but still nothing changes


as always ... verify what you think you are working with. hint (input-target) is incorrect

Quentin Le Guennec09:10:04

Hello, clojurescript expects me to quote dest-ns in (ns publics dest-ns) line 3, which is kind of complicated to do:

(defmacro export-library-cljc! [dest-ns exclude-symbols]
     ~@(for [[intern-name] (ns-publics dest-ns)]
         (when-not (contains? exclude-symbols (symbol (str intern-name)))
              ~(when (contains? (ns-interns 'clojure.core) (symbol (str intern-name)))
                 `(ns-unmap *ns* (symbol ~(str intern-name))))
              ~`(defalias ~(symbol (str dest-ns "/" intern-name))))))))
The macro is working perfectly in clojure, can someone explain?


Clojure 1.10.0
user=> (doc ns-publics)
  Returns a map of the public intern mappings for the namespace.
ClojureScript 1.10.879
cljs.user=> (doc ns-publics)
  Returns a map of the public intern mappings for the namespace.
In CLJ, it's a function. In CLJS, it's a macro. Why exactly this difference exists, I don't know. Probably due to how some things in CLJS simply don't exist in run time, like vars.

Quentin Le Guennec09:10:47

Oh, I see. Which means it'll be already quoted in clojure but not in clojurescript.


Your conclusion is the other way around - it will be quoted in CLJS since it's a macro. Fortunately for your implementation, the CLJ version of ns-publics also accepts a symbol. So you can just use a symbol there at all times. Might still be tricky given that one is an fn and the other is a macro.

Quentin Le Guennec09:10:10

Yeah, quite tricky


again a question about react keys I see now this error :

template.cljs:413 Warning: Every element in a seq should have a unique :key: ([ {:label "Today", :value nil}] [ {:label "Tomorrow", :value nil}])
 (in )


Can I solve this with this :

^{key 'today'}{:label "Today", :value nil}]
if not, please give me the right answer . This is bugging me for several days


Have you gone through the links I provided before? One of them was with an example from the Reagent's website.


but that one was not given a example of how to add a key before a {}


I don't understand what you mean. Why would you need to add it in front of {}?


The warning clearly mentions

([ {:label "Today", :value nil}]
 [ {:label "Tomorrow", :value nil}])


It talks about a seq, and that above is that seq.


It talks about every element in the seq, and an element of that seq would be each Hiccup vector.


Then Im confused


I did it here

(defn title []
  [:h1 (:title @app-state)])

(defn temperature [temp]
  [:div {:class "temperature"}
   [:div {:class "value"}
    (:value temp)]
   [:h2 (:label temp)]])

(defn postal-code []
  [:div {:class "postal-code"}
   [:h3 "Enter your postal code"]
   [:input {:type "text"
            :placeholder "Postal Code"
            :value (:postal-code @app-state)
            :on-change #(swap! app-state assoc :postal-code (-> % .-target .-value))}]
   [:button {:on-click get-coordinates} "Go" ]])

(defn app []
  [:div {:class "app"}
   [:div {:class "temperatures"}
    (for [temp (vals (:temperatures @app-state))]
      ^ {key [temperature temp]}[temperature temp])]


the part that the error message is talking about looks like this

(defonce app-state (r/atom {:title "WhichWeather"
                            :latitude 0
                            :longitude 0
                            :postal-code ""
                            :temperatures {:today {:label "Today"
                                                   :value nil}
                                           :tomorrow {:label "Tomorrow"
                                                      :value nil}}}))


In your code, that seq above is generated with this code:

(for [temp (vals (:temperatures @app-state))]
  [temperature temp])
A key in React represents an identifier for an element within its parent element - unique within the scope of that parent. It needs to remain stable, it can't be just the index. In your case, such a stable identifier seems to simply be the key in the :temperatures map. Here's a solution for your particular case:
(for [[temp-id temp] (:temperatures @app-state)]
  ^{:key temp-id}
   [temperature temp])


But given that: • Maps are inherently unordered • You just have two items in that map here's how I'd rewrite it:

(let [{:keys [today tomorrow]} (:temperatures @app-state)]
  [temperature today]
  [temperature tomorrow])
The code doesn't even need :key anymore.


> the part that the error message is talking about looks like this The error you posted in the main channel clearly doesn't talk about that defonce at all. Why do you think the definition of your app-state is relevant here?


oke, so that key instead of the for loop


that code is given by the book


I think it is relevant because it pointing to that code label: Today : value nil


That's just the value of temp. It doesn't talk about your actual code, it only talks about the value it receives. It's a run-time warning, not a compile-time one - it doesn't check your code, it only checks the values.


> that code is given by the book Right. Not a great look for that book TBH.


I know. we discussed that earlier


I tried that key thing but still the same runtime error


(defn app []
  [:div {:class "app"}
   [:div {:class "temperatures"}
    (for [temp (vals (:temperatures @app-state))]
      ^ {key  temp}[temperature temp])]


What you wrote is completely different from what I wrote.


:key is not the same as key. temp is not the same as an identifying value.


Use my code. Confirm that it results on no warnings. Then go through the React documentation on keys while keeping my code in mind, along with the fact that ^{:key x} in front of a Hiccup vector results in assigning a React key to the resulting React element. BTW that ^{...} construct just assigns metadata. So ^{...} [...] is sugar for (with-meta [...] {...}). And Reagent uses :key metadata to assign keys to React elements.


so this instead of the for loop

(let [{:keys [today tomorrow]} (:temperatures @app-state)]
  [temperature today]
  [temperature tomorrow])


hmm, this breaks the layout

(defn app []
  [:div {:class "app"}
   [:div {:class "temperatures"}
    (let [{:keys [today tomorrow]} (:temperatures @app-state)]
  [temperature today]
  [temperature tomorrow])]


No, I meant the loop, given that it's the only one of the two code examples I provided that still needs the :key.


The layout breakage is surprising, given that the resulting markup should be exactly the same.


I did use the loop and that one works perfect without any error messages


maybe I have to find a better book/tutorial to learn clojurescript


The ClojureScript website has a selection of materials:


I do not see why mu right blue area is not updated here :