This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-07-17
Channels
- # announcements (2)
- # babashka (1)
- # beginners (54)
- # calva (19)
- # clj-kondo (7)
- # cljs-dev (6)
- # cljsrn (18)
- # clojure (11)
- # clojure-europe (19)
- # clojurescript (7)
- # conjure (3)
- # cursive (7)
- # datomic (4)
- # figwheel-main (2)
- # fulcro (4)
- # helix (5)
- # honeysql (24)
- # instaparse (2)
- # lsp (20)
- # malli (17)
- # off-topic (6)
- # pathom (2)
- # practicalli (2)
- # shadow-cljs (2)
- # tools-deps (34)
- # vim (7)
Trying to get data from an API and display it into a web page. I can pull the data using cljs-http, I can view the data in the REPL, but when I try to display the data in the webpage, it shows nothing (and throws no errors). Any idea what I might be doing wrong? Here’s the code:
(def helium-api "MY_API_URL")
(def api-data (atom {}))
(defn get-data
[]
(go (let [response (<! (http/get helium-api
;; parameters
{:with-credentials? false}))]
(reset! api-data (:body response)))))
(get-data)
(def helium-status (-> @api-data :data :status :online))
(defn home-panel
[]
[:div
[:h1
{:class (styles/level1)}
(str "Helium status is " helium-status ".")]])
In REPL, there's a delay between (get-data)
and (def helium-status ...)
.
But when you use it as a whole, there's none - the request isn't finished by the time you try to access the results in @api-data
.
go
is asynchronous - it returns immediately, without blocking.
Assuming you're using Reagent, do all these steps:
• Don't store the data in helium-status
• Switch from the built-in atom
to the reagent.core/atom
one
• Use the ratom directly in home-panel
This way, when the data comes, home-panel
will be automatically re-rendered.
Check out Reagent multiple examples and documentation for the details.