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 ?
The console for the Client UI is emitting the error
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
No problem you’ve been great! Thanks I may give lamdaisland a try tonight
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
Ok cool I will look at those tools