This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-05-16
Channels
- # announcements (22)
- # beginners (4)
- # biff (4)
- # cider (5)
- # clerk (3)
- # clojure (28)
- # clojure-chennai (1)
- # clojure-europe (23)
- # clojure-gamedev (7)
- # clojure-korea (5)
- # clojure-madison (3)
- # clojure-my (1)
- # clojure-nl (1)
- # clojure-norway (49)
- # clojure-sweden (7)
- # clojure-uk (4)
- # clojuredesign-podcast (14)
- # clojurescript (10)
- # clr (5)
- # cursive (4)
- # datascript (17)
- # datomic (2)
- # events (1)
- # garden (1)
- # introduce-yourself (2)
- # jobs-discuss (14)
- # lsp (23)
- # malli (14)
- # missionary (9)
- # off-topic (109)
- # overtone (7)
- # polylith (5)
- # releases (5)
- # shadow-cljs (7)
- # sql (13)
- # testing (30)
- # xtdb (10)
- # yamlscript (44)
Hello
I’m trying to rewrite this example on ClojureScript http://reactcommunity.org/react-transition-group/switch-transition, but when I add :node-ref
to CSSTransition
it stops working:
(defn app []
(let [state (r/atom true)
node-ref (r/atom nil)]
(fn []
[:> SwitchTransition
[:> CSSTransition {:key @state
:class-names :fade
:timeout 500}
[:div {:ref (fn [node] (reset! node-ref node))}
[:button {:on-click (fn [_] (swap! state not))}
(if @state
"Hello, world!"
"Goodbye, world!")]]]])))
I just add :node-ref @node-ref
. Any idea of how I can use refs
?I don’t think so
> nodeRef
> A React reference to the DOM element that needs to transition: https://stackoverflow.com/a/51127130/4671932
why not just use a React ref with createRef
?
i recently also implemented reagent component that uses CSSTransition. i can't remember exact details why i got node-ref working with ref hook but not with createRef
, maybe it was something wrong with my state management. i wanted to use :addEndListener
to control animation length from CSS.
so something like
(let [node-ref (react/useRef nil)]
[:> CSSTransition {:nodeRef node-ref ...}])
and wrap as function component [:f> ...]
when usingI want to use more than 3 elements that use that transition, so I was thinking of using a map to store the refs
depending on the key. The problem comes when I try to translate this part:
const helloRef = React.useRef(null);
const goodbyeRef = React.useRef(null);
const nodeRef = state ? helloRef : goodbyeRef;
Because of the state ? helloRef : goodbyeRef
partI also observed that when using a ratom
it enters a loop, since when :ref
is invoked to store the reference, this subsequently renders the component again because it is used as a parameter in the CSSTransition
Here is the actual implementation:
(defn animation []
(let [state (r/atom false)
hello-ref (useRef nil)
goodbye-ref (useRef nil)]
(fn []
(let [node-ref (if @state hello-ref goodbye-ref)]
[:> SwitchTransition
[:> CSSTransition {:key @state
:node-ref node-ref
:add-end-listener (fn [done]
(-> node-ref .current (.addEventListener "transitionend" done false)))
:class-names :fade}
[:div {:ref node-ref}
^{:key @state}
[:button {:on-click (fn [_] (swap! state not))}
(if @state
"Hello, world!"
"Goodbye, world!")]]]]))))
(defn app []
[:f> animation])
But I get this error:
Error: Rendered fewer hooks than expected. This may be caused by an accidental early return statement.
😞I don't think it will necessarily solve your issue but I do believe that refs return a JS object with a current
field, so you should be accessing it with .-current
and not .current