Fork me on GitHub
#helix
<
2023-01-16
>
pbranes16:01:28

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))))

geraldodev17:01:54

Isn't the response showing a message from the server side ? What is the :status for the response ?

pbranes18:01:43

core.cljs:200 {:status 200, :success true, :body "testing", :headers {"content-length" "7"}, :trace-redirects ["" ""], :error-code :no-error, :error-text ""} (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")

geraldodev18:01:53

Which console log is emitting the error ? Could you take a picture for more context ?

pbranes18:01:04

The console for the Client UI is emitting the error

pbranes18:01:40

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")))

pbranes19:01:32

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.

pbranes19:01:41

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))))

geraldodev19:01:46

Sorry not replying sooner, I joined a meeting

pbranes19:01:45

No problem you’ve been great! Thanks I may give lamdaisland a try tonight

geraldodev19:01:51

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

pbranes19:01:07

Ok cool I will look at those tools