Fork me on GitHub

Has anyone used a state management system other than Reagent's ratom/track/cursor with Reagent? My original plan was to talk to React directly, but looking at Reagent internals I am thinking that might not be so easy. So I have to find a way to trigger renders selectively from my own state tool. One option is simply to associate a ratom "counter" with each reactive element, then have my state manager bump the counter. Or I could go with the reagent form-3 components on which I think I can use forceUpdate. I see that in the reagent internals anyway. Thoughts/prior experience welcome!


FWIW, if you want just the Hiccup from Reagent and the rest from React, you would definitely be better off using something like


Ha-ha, I have been thinking all I would need to do is ditch Hiccup and JSX, which I have done by using r/create-element, but now I have to force rendering when my state management lib Cells says so. But this is so awesome. Helix was completely off my radar. Found a problem with their TodoMVC in which one mouse-click causes two renders, a classic glitch in which the first render is undesirable, but I wager that can be fixed with a bit of elbow grease. And I am relenting on the no-Hiccup thing: if I can wire up Cells within a form-3, MatrixRN will look more like what Clojurians, a notoriously conservative bunch, are used to. Thx a ton for the Helix dope-slap! :face_palm:

ūüĎć 2

Hmm. Why is Helix not on (That's my excuse. ūüôā )


Oh, actually, the only reason I recommended Helix is because I thought it used Hiccup. :D But I mistook it for its predecessor, Yeah, Hiccup looks nice and in some cases "it's just data" is indeed useful, but it still has well known downsides.


ha-ha again, my head is spinning, did not realize Helix does not use Hiccup! Which is fine, that is how Matrix rolls:


In reagent, instead of adding the on<event> = (fn [evt]....) in side hiccup, if I prefer to use the (.querySelector js/document "#widget") , how do I get an event which tells me that the widget has finished rendering in DOM such that the querySelector call will return a valid object?


That's a very bad preference in React world in general. My first advice would be to get rid of it and not look back.


Is there some problem where you think that :on-%event% is not sufficient?


And if you don't need a particular event but rather the DOM node itself (needed e.g. when you interop with some JS library), you should use React refs instead of .querySelector.


trying to get some architectural beauty... by keeping all the hiccup just for the view and all the script using queryselector... also it is hard to use the finally clause with the with-let to unconnect the event handlers... because you can't use anonymous functions what is a react ref? how do you use it?


Why do you need to remove the handlers?..


> what is a react ref? how do you use it? It's a very well documented concept.


When a DOM node is removed, all its event handlers are moved automatically. Unless you use them somewhere else - standard GC stuff.


So e.g. for a button, just do this:

(defn my-button [{:keys [on-click label]}]
  [:button {:on-click on-click}
No need to invent anything here, it's all very simple.


Unless I'm mistaking you for someone else - have you went through Reagent examples, like I have suggested before?


no you are not mistaken... It was me... There are a lot of the examples... all over the web... I've been trying to go thru the code but it is not the easy to find the solutions to my specific problems...


I meant specifically these examples, within the repo:


The main page has a lot of nice and simple examples:


The react component format I am using ... (don't know if this is form 2 or not...) : (defn ReactComponent [<params>] (with-let [comp_state (atom {...})] .... (finally ...<cleanup>...))) The examples I've seen so far they always unlistenevent handler in the finally clause. Which sounds sensible.. to me... is this not how one should go about components?


Here you should read at least the titles of the documents, just to know what exists and where to look for it:


What I understood from your explanation is that I don't need the finally clause at all... is that right?


ok thanks for the links... I'll have a look more thoroughly.


removeEventListener should be called explicitly only when you have called addEventListener before to something that will be left there after your component is removed. Often, it's the js/document itself. If it's the very same component that gets unmounted, then you don't need to do any cleanup.


> What I understood from your explanation is that I don't need the finally clause at all... is that right? Depends on what exactly you're doing. I need a full(er) example to tell.


ok so if instead I use just the on<event> = (fn [] ...) I don't need to do removeEventListener?


Look I am not ashamed to admit I am quite green. I thank you for your patience. I am trying to wrap my head around how to actually structure the code to do what I want it to do... please bear with me...


I don't know what you mean with that = there. But if it's like with the my-button above, then no - you don't need to remove anything. Even if you want to remove some event handler from a component that's still mounted - just set that value to nil or remove the key altogether from the attributes map, that's it! React will do the rest for you.


it is the hiccup code


But you don't use = in Huccup...? If you do - how?


in hiccup code you define that as :on<event> (fn [evt] ...) right?


sorry in HTML it becomes =


Usually :on-click, but :onClick should work as well.


yes the event is not important... I am more intested in the best practice of how to structure the code...


It doesn't become = because those event handlers are attached by Reagent in JS - they never make it to the markup.


really? I didn't know that


Well, attach :on-click and see what's on your page in the Inspector. ;)


I will take your word for it... Sensei!! ūüėĄ like I keep saying I am a complete numpty


That's alright, we all start somewhere.


but I am a stickler for the best practices and how architecture translates to the code structure... somehow I have trouble understanding anything without it... ūüėĄ


so again... if I use just hiccup to connect events... I don't need to removeListener later in the finally clause.... right?


Reagent componet defined as following: (defn reagentComponent [params] (with-let [compstate (atom {...}) randomchannel (chan) chan_hndlr (go-loop [] (let [val (<! ch)] (if (= val nil) ((prn val) (recur)) nil)] on the line which defines go-loop... I keep getting No implementation of method: :emit-instruction of protocol: #'cljs.core.async.impl.ioc-macros/IEmittableInstruction found for class: cljs.core.async.impl.ioc_macros.Jmp error. What is wrong? I've included the core.async in the ns as well. Is it not allowed to define go-loop in with-let construct? I am using clojurescript(obviously)... is there some limitation to this.


One minor thing: replace (= val nil) with (nil? val). One major thing: ((prn val) (recur)) evaluates (prn val) and tries to call the result as a function. In the context where (recur) is used as an argument, whereas recur is not a statement - it's a special construct. Seems like you've forgotten do there: (do (prn val) (recur)).


Also, when you return nil from one of the if branches, you can just use when. Then you also don't need the do because it's implicit with when.


FWIW, given that you're a complete beginner, I genuinely think that you should avoid dealing with core.async for now. Leave it till you understand and are able to write more or less complex Reagent apps. Or vice versa - learn core.async first and then add Reagent. Don't learn multiple things all at once.


Finally, a meta-remark - use triple backticks when embedding code blocks instead of singular ones. So that it looks like this:



yay!! it worked!! my finaly go-loop block is now \\\(go (loop []                                      (let [val (<! logchan)]                                        (when (complement (nil? val))                                          (prn val)                                          (recur)))))\\\


triple backticks didn't work...


Backticks, not slashes. This thing - ` On my keyboard, it's where the tilde key is, under Esc.


Replace complement with not.


Or use when-not.


oh... lets try again...

(go (loop []
                                     (let [val (<! logchan)]
                                       (when (complement (nil? val))
                                         (prn val)


Also, read up on when-let, when-some, if-let, if-some. In your case, when-some would be helpful.


ok new code...

(go (loop []
                                     (let [val (<! logchan)]
                                       (when-not (nil? val)
                                         (prn val)


Oh, right - and (not (nil? x)) is just (some? x). :)


Clojure standard library's API is not that large - very well worth reading, along with the examples, at


I have soooo much to learn!!! If you don't mind me asking ... how long have you been working on Clojure/script/reagent etc?


Around 5 years.


many many thanks

ūüĎć 3