This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-01-16
Channels
- # announcements (2)
- # babashka (51)
- # beginners (165)
- # biff (39)
- # clara (1)
- # clj-kondo (20)
- # cljsrn (6)
- # clojure (64)
- # clojure-belgium (11)
- # clojure-conj (2)
- # clojure-europe (12)
- # clojure-nl (3)
- # clojure-norway (7)
- # clojure-uk (6)
- # clojurescript (11)
- # conf-proposals (1)
- # conjure (1)
- # core-async (19)
- # cursive (6)
- # data-science (16)
- # datomic (6)
- # deps-new (4)
- # fulcro (60)
- # funcool (3)
- # graalvm (9)
- # helix (14)
- # introduce-yourself (4)
- # jobs-discuss (13)
- # joyride (1)
- # kaocha (2)
- # malli (12)
- # off-topic (25)
- # polylith (9)
- # portal (3)
- # practicalli (1)
- # rdf (43)
- # re-frame (7)
- # reagent (5)
- # releases (5)
- # remote-jobs (8)
- # sci (5)
- # shadow-cljs (42)
- # squint (6)
- # xtdb (5)
Hello, I have a clojure server and a shadow-cljs client. I am trying to fetch a string(value is "testing") from the server and put it on a page. I set the initial state for the page to render and then in the use-effect try to update when the promise gets resolved. The console log is reporting that there is a "error in the <div>": (:require [helix.core :refer [$ <> defnc]]
[cljs.core.async :refer [<! chan]]
[cljs-http.client :as http]
[helix.hooks :as hooks]
[helix.dom :as d]
[promesa.core :as p]))
(defn make-remote-call [endpoint ]
(go (let [response (<! (http/get endpoint {:with-credentials? false}))]
(:body response))))
(defnc cron-page []
(let [
[state set-state] (hooks/use-state {:value "init state"})
fetch (fn [] (p/let [response (make-remote-call "http://localhost:3000/testing")]
(set-state assoc :value response)))
]
(hooks/use-effect []
:once
(do (js/console.log "use fx")
(fetch)))
(d/div (:value state))))
Isn't the response showing a message from the server side ? What is the :status for the response ?
core.cljs:200 {:status 200, :success true, :body "testing", :headers {"content-length" "7"}, :trace-redirects ["
(defn make-remote-call [endpoint ]
(go (let [response (<! (http/get endpoint {:with-credentials? false}))]
(prn response)
(:body response))))
(make-remote-call "http://localhost:3000/testing")
Which console log is emitting the error ? Could you take a picture for more context ?
Below is my code with log statements and a img of the console:
(defn make-remote-call [endpoint ]
(go (let [response (<! (http/get endpoint {:with-credentials? false}))]
(prn (str "inide go " response))
(:body response))))
(defnc cron-page []
(let [
[state set-state] (hooks/use-state {:value "init state"})
fetch (fn [] (p/let [response (make-remote-call "http://localhost:3000/testing")]
(prn (str "in fetch " response))
(set-state assoc :value response)))
]
(hooks/use-effect []
:once
(do (js/console.log "use fxx")
(fetch)))
(js/console.log (str "state " state))
(d/div "hello")))
I would up for alternative solutions instead of using cljs.core.async, cljs-http.client and promesa to fetch data from the server. I feel the real issue is my lack of knowledge with these libs.
Think I got it:
(defn make-remote-call [endpoint fn-state]
(go (let [response (<! (http/get endpoint {:with-credentials? false}))
data (:body response)]
(fn-state data))))
(defnc cron-page []
(let [[state set-state] (hooks/use-state {:value "init state"})]
(hooks/use-effect
[]
:once
(make-remote-call "" (fn [data] (set-state assoc :value data))))
(d/div (:value state))))
https://github.com/lambdaisland/fetch might interest you
Sorry not replying sooner, I joined a meeting
You might be interested in https://github.com/henryw374/fetch which is a fork a lambdaisland that preserves the interface but is more sensible about dependencies... Thats a testimony that the interface is good. https://github.com/lambdaisland/fetch/issues/18