Clojure script. Must have a unique key

Hello

I have this code from the learn-clojurescript book
But on my browser I see a message that today and tomorrow need a unique key
How do I solve 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}}}))

There appears to be nothing wrong with that map. I just evaluated it at the repl and it looks fine. Maybe you had a bad editing session, or there is more information about the error you can share.

2 Likes

As discussed on Clojurians Slack, it speaks about React key and the error doesn’t stem from this code - it’s caused by using for in a Reagent view function.

Relevant links:

2 Likes

I see it

it must be like this

^{:key item} [:li "Item " item])])

and I think now that i has to be placed here :

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

clojurescript is for a beginner not easy. Im thinking of giving up here

So maybe this way

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

You’re doing a great job - learning any new language can be frustrating in the beginning. Stick with it and you won’t regret it.

1 Like

could be but now the site is totally not working.
I see only a blue screen and nothing more and no error messages

and after restarting my computer localhost:9500 do not give a site at all

I see this on my wsl prompt

Results: Stored in vars *1, *2, *3, *e holds last exception object
[Rebel readline] Type :repl/help for online help info
Opening URL http://localhost:9500
Failed to open browser: 
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
[Figwheel] Compiling build dev to "target/public/cljs-out/dev-main.js"

opened the localhost:9500 manually and see a message that no server is found

oke, got it running on Chrome
and still see this :

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

how do I add a key there

Can I do :

^{key 'today'}{:label "Today", :value nil}]
(defn app []
  [:div {:class "app"}
   [title]
   [:div {:class "temperatures"}
    (for [temp (vals (:temperatures @app-state))]
       ^ {key temp} [temperature temp])]
   [postal-code]])

The above looks correct, apart from the key part - needs to be a keyword, so ^{:key temp}.

Thanks, that solved it

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.