Fork me on GitHub
#clojurescript
<
2022-12-18
>
Jason Lee10:12:43

I noticed the reagent-project have js/undefined on the tail when every time call react/useEffect I wonder what's this all about?

Jason Lee10:12:04

@U061V0GG2 Maybe shed some light on this for me, thanks.

thheller10:12:53

useEffect uses the return value if its a function. maybe this is just to ensure no unwanted effects happen. just a guess though

Jason Lee10:12:42

I thought useEffect can return a function to do some cleanup, so this may prevent the cleanup to take effect.

juhoteperi10:12:49

React requires the return value is always either a function or undefined: https://github.com/reactwg/react-18/discussions/95

Jason Lee10:12:37

Thanks. I finally understand it now.

DerTev17:12:14

Any idea, why I get this error? I use shadow-cljs 2.20.14

p-himik17:12:12

If you search for it online or here, you'll get about a bazillion results with tips. ;) Just add ^js in front of x.

DerTev17:12:16

Ah sorry, just found answers saying it should be fixed with the newest version.

DerTev16:12:33

Any idea, why this doesnt work?

10 |   (async/go (swap! current-post assoc
---------^----------------------------------------------------------------------
 Cannot infer target type in expression (. inst_12835 -author)
--------------------------------------------------------------------------------
  11 |                    :author-name (get (async-interop/<p! (pb/get-record "users"
  12 |                                                                        (.-author ^js @current-post)
  13 |                                                                        [])) :name)))

DerTev16:12:40

I mean it should be right?

thheller16:12:51

unfortunately the go macro loses type hints

DerTev17:12:41

Uhm, and how do I have to do it then?

DerTev17:12:09

I mean it doesnt works with

(def current-post ^js (r/atom nil))
either...

thheller17:12:22

write an accessor function and use that

thheller17:12:53

(defn get-author [^js x] (.-author x)) or so

thheller17:12:08

or don't use go, which is what I would recommend

DerTev23:12:48

Thanks! And is there a way to run the async-stuff sync?

thheller06:12:02

no, that is impossible in JS

DerTev16:12:04

So, currently I have

(defn open-post [post-id]
  (shadow-modern/js-await [post (pb/get-record "posts" post-id)]
                          (println (str "post: " post))
                          (shadow-modern/js-await [author-name (->> ^js post
                                                                    .-author_name
                                                                    (pb/get-record "names"))]
                                                  (println (str "author-name: " author-name))
                                                  (reset! post-view/author-name author-name))
                          (reset! post-view/current-post post)))
and I get the following output: [img] Result: <author-name> is printed, when the page renders. But I want, that author-name is changed, before post-view is rendered. Is this possible in any way?

DerTev16:12:20

(post-view is rendered by current-post btw)

DerTev13:12:30

How would you do smth like this?

thheller14:12:58

sorry I do not have enough information about what you are doing to answer

thheller14:12:08

I'd say you have to wait to render anything until you get the name

thheller14:12:12

or display a placeholder

DerTev14:12:54

Ah, thats a good idea, thanks!