Fork me on GitHub

I'm trying to figure out how to make a component respond to events other than user interaction i.e idleness and am wondering if there's a more appropriate way to do the following -

(defn countdown []
      (r/with-let [seconds-left (r/atom 60)
                   timer-fn     (js/setInterval #(swap! seconds-left dec) 1000)
                   arbitry-thing (atom false)
                   [:div {:on-change (if (<= @seconds-left 55) (timer-handler @seconds-left "time has elapsed" (reset! arbitry-thing true))
                                                                (js/console.log @seconds-left))} "Time Remaining: " (str @seconds-left "" @arbitry-thing)]]
                  (finally (js/clearInterval timer-fn))))
while this works, I get warning from React, stating that on-Change listener is returning a bool rather than a function


The value of :on-change should be a function. It’s now being set to the value returned by the if expression.


You can for example put a hash # in front of the if-expression to make it an anonymous function.


However I don’t know what would be changing the :div in this example. I guess you’re attempting to just display different content depending on how much time has elapsed. You don’t need :on-change for that. You can just wrap the hiccup you want to show into the if-expression.


There’s actually a timer example on the reagent docs front-page


To add to the above comments - if you need to know whether the user has been completely idle, i.e. no keyword or pointer events (which include mouse and touch events) on the page at all, then you need to listen to all those events. For that, you need to use addEventListener on either js/document or js/window with its capture option set to true - otherwise, event handlers that use stopPropagation or that don't bubble for some other reason (like scrolling a scrollable container) will not reset the idle timer.


Yes, the whole scenario here is that the timer would not be static like this and would be reset with user interaction, like a click etc. It's not really the timer itself that is the question, it's just where to fire off a function (that is looking at a variable, in this case an atom) that isn't changing the layout of the component.


Well yeah, that's exactly what the aforementioned timer example does, so seems like you have all the building blocks you need.


Grand, thanks. I just wondered if there was a better place to do that kind of work. The :on-change didn't feel like it would work generically. I don't think that timer example touches any of that.


That why I wrote that comment about addEventListener. What you want to achieve has to be outside of any :on-change or anything like that because there's no built-in declarative way to have capturing event listeners.


Yes, I'm wondering if an eventlistener is actually the correct way to track that value at all even though it works. Really I'm looking at the value of the atom rather than the change of the text in that div.


No, hold on. It's not for tracking the value. It's for resetting it back to 0 upon any user interaction. At least, that's how I understand your usage of the word "idleness". So you have a timer somewhere that, when it reaches e.g. 1000 seconds, does something. And then you have all those capturing event listeners that simply reset that timer.


Ok, I was just using this as an example, for a wider concern. But in this case, I see what you mean.


Thanks for your time @U2FRKM4TW & @U6N4HSMFW!

👍 2

Sorry just to follow up, I don't actually understand where to manually add an eventlistener to a component then


In the root component. Make it a form-3 component and add all the event listeners in its :component-did-mount lifecycle function and remove the events in the :component-will-unmount - even though in your case it seems like it's not necessary to remove them at all, it's still a good habit. Alternatively, instead of using a form-3 component, use reagent.core/with-let with a finally clause.


ah great, when reading through everything, it kind of felt like those kind of components were referred to only before hooks came along and that they weren't really to be used anymore.


Nah, not really. I still haven't used a single hook in any of my projects. :)


At least from what I know, React hooks largely solve problems that have been solved by Reagent quite some time ago.


Im trying to find an example of grabbing a specific component but could I use a ref rather than the class


Not sure what you mean.

Ben Lieberman22:10:00

I have an event listener on window that makes a request to my server on page load. It works like I want it to. But I have routing set up with reitit.frontend and if I navigate to another page and then return, the server call is not performed again. I am using r/with-let and my event listener is registered inside the block. Is that the problem?


Assuming your router is set up correctly, you have a proper SPA - meaning, the tab/window is not reloaded upon navigation, so the event listener is not fired. However, you can listen to a different event. If your router is set up to use hashes, you can listen to If it's set up to use HTML5 history API, then


Alternatively, maybe you're using venantius/accountant along with reitit - then, I believe, you can set it all via a single function, :nav-handler.

Ben Lieberman23:10:38

I'll give Accountant a try. Thanks!


See also You can dispatch events (to make server requests, etc.) from the start and stop controllers associated with each route.