This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-09-24
Channels
- # announcements (7)
- # babashka (5)
- # babashka-sci-dev (20)
- # beginners (125)
- # biff (98)
- # catalyst (1)
- # clerk (37)
- # clj-kondo (6)
- # clojure (49)
- # clojure-dev (18)
- # clojure-europe (6)
- # clojure-uk (2)
- # data-science (17)
- # deps-new (20)
- # emacs (11)
- # helix (5)
- # hyperfiddle (34)
- # malli (3)
- # missionary (4)
- # reitit (4)
- # sci (15)
- # solo-full-stack (7)
- # sql (5)
- # testing (2)
When I use the setter inside use-effect, I can see the value of div is 10 initially, And I can also see console print "running use-effect", but the value of div is still 10 not the intended 20 What did I do wrongly?
(defnc app []
(let [player-ref (hooks/use-ref nil)
[subtitle set-subtitle] (hooks/use-state 10)
_ (def debug subtitle)]
(hooks/use-effect :always
(fn []
(js/console.log "running use-effect")
(set-subtitle 20)
))
(<>
(d/h1 {:class "my-8 flex justify-center items-center text-2xl font-bold"} "Sentence Relationships!!")
(d/div "subtile: " subtitle)
)))
(defnc Tmp []
(let [[attr setter] (hooks/use-state 10)]
(hooks/use-effect :once (fn []
(setter 20)))
(d/div (str "this is tmp comp: " attr))))
for this simple example, i will expect it will render a div with 20, but it turns out it's a div with 10
from what i know, the procedure is first render -> run useEffect -> update state -> trigger rerender -> get a div with 20
didn't know what i missed here.The body does not need to be wrapped in a function. so you have effectively written the following:
(defnc Tmp []
(let [[attr setter] (hooks/use-state 10)]
(hooks/use-effect :once nil (fn unmount []
(setter 20)))
(d/div (str "this is tmp comp: " attr))))
1