This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-05-22
Channels
- # ai (1)
- # announcements (1)
- # babashka (9)
- # beginners (18)
- # calva (19)
- # clerk (136)
- # clj-http (3)
- # clj-kondo (13)
- # cljs-dev (166)
- # clojure (39)
- # clojure-europe (133)
- # clojure-nl (1)
- # clojure-norway (5)
- # clojure-uk (12)
- # clr (1)
- # community-development (6)
- # conjure (8)
- # cursive (13)
- # data-science (1)
- # datomic (26)
- # events (5)
- # fulcro (12)
- # gratitude (3)
- # honeysql (9)
- # hyperfiddle (33)
- # introduce-yourself (6)
- # kaocha (1)
- # lambdaisland (5)
- # malli (4)
- # off-topic (3)
- # rdf (4)
- # re-frame (3)
- # releases (3)
- # scittle (11)
- # specter (2)
- # sql (4)
- # tools-deps (4)
- # vim (10)
Are there any examples of using react hooks to handle events (I want to capture resize and key input events but any will help)?
(defsc HookThing [this props]
{:query [:thing/id] ; normal Fulcro component options
:ident :thing/id
:use-hooks? true ; generate a hooks-compatible function instead of a React class.
}
(hook/useEffect
(fn []
(defn handleResize
[]
(js/console.log "test")))
(.addEventListener js/window "resize" handleResize))
)
(def ui-hook-thing (comp/factory HookThing {:keyfn :thing/id}))
I tried this based on js code but couldn't seem to get it to work. Any help/guidance on handling events would be great, thanks!Read the react docs. My guess is you’re getting an error in the js console. useEffect must return undefined or a teardown function, and you should also prevent it from running every render (dependency list). If you insist on doing this kind of thing in the UI, then you probably want Fulcro’s wrapper “use-lifecycle” instead, which is a little nicer notation for doing one thing on mount, and then a different one on unmount.
Thanks! I'll try get it working as is, and then move on to learning the state machine 🙂
(hook/use-lifecycle (fn []
(defn handleResize
[]
(resize this))
(.addEventListener js/window "resize" handleResize)
(resize this)))
still not quite right. You want a teardown fn that uninstalls that listener, AND you want to use (fn [] …) instead of doing a side-effect top-level defn
inside of this thing…
(hook/use-effect
(fn []
(let [handler #(resize this)]
(.addEventListener js/window "resize" handler)
(fn [] (.removeEventListener js/window "resize" handler)))
[])
The empty vector makes it work like use-lifecycle, but you actually want use-effect because you need to remember the exact handler you installed, so you can remove it (which is why you wanted the defn?).
Thank you for all your help! I am working of example "Example 6. https://book.fulcrologic.com/#DrawinginaCanvas" and I get it all working if I apply the use-effect directly to "Child" but whenever I add use-effect? true
to the component, it causes the local :coords
to return null
. Is this expected behavior?
@U0CKQ19AQ Just wanted to follow up if this was a bug or intentional?
@U0558HTJE6Q you need to read the React docs. Fulcro has nothing at all to do with refs or hooks, AND the option is use-hooks?
not use-effects?
. Fulcro just passes these things along directly to react and does absolutely nothing to them. If it isn’t working, the problem is in your code and understanding of React.
The example code you are referring to is not using hooks, so useEffect isn’t part of the equation at all
It’s using ref
, which can call the function with nil
. The ref is only available once the DOM node is actually mounted. The way react works is:
1. Run your “render” code to get elements that are meant to go on-screen
2. diff the DOM with your desire
3. Draw the DOM
Once the DOM is drawn the ref function can be called with a real value of the DOM node.