helix

pbranes 2023-01-16T16:09:28.934119Z

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

2023-01-16T17:41:54.398419Z

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

pbranes 2023-01-16T18:06:43.192019Z

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

2023-01-16T18:21:53.050139Z

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

pbranes 2023-01-16T18:25:26.163559Z

pbranes 2023-01-16T18:26:04.564199Z

The console for the Client UI is emitting the error

pbranes 2023-01-16T18:55:40.588169Z

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

pbranes 2023-01-16T19:06:32.806109Z

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.

pbranes 2023-01-16T19:22:41.088729Z

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

2023-01-16T19:44:17.202829Z

https://github.com/lambdaisland/fetch might interest you

2023-01-16T19:44:46.523089Z

Sorry not replying sooner, I joined a meeting

pbranes 2023-01-16T19:51:45.314899Z

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

2023-01-16T19:54:51.671889Z

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

pbranes 2023-01-16T19:59:07.908599Z

Ok cool I will look at those tools