reagent

bibiki 2023-04-06T14:46:42.819119Z

hi, I'm having a what I'd think small problem with reagent. here is the context and the problem: I have a side menu that lists various types. when i click on a type, a list of items of that type shows to the right of the side menu. that works pretty well. however, now that i introduced retrieving items of type from the database, the view that shows items is misbehaving as it is not clearing the view of the earlier items, it just adds newly selected type's items below the previously selected type's items. I am sure that I am using a reagent atom, and that I am swapping it correctly. the problems seems to be related to the fact that I am updating my reagent atom that contains my state from a promise handler. I am using a promise handler to retrieve my data as follows: (ajax.core/GET "/items" {:handler #(swap! app-state assoc items %)}) would somebody please guide me how to redeem this situation

p-himik 2023-04-06T14:47:30.487199Z

The first step would be to post your actual code.

p-himik 2023-04-06T14:47:52.349599Z

Also that swap! call is suspicious - what is items in there?

bibiki 2023-04-06T14:49:03.401749Z

my app-state has shape {items [item1, item2, ...], some-other-key some-other-value}

bibiki 2023-04-06T14:50:16.370539Z

and here is what Id say is the relevant code you asked for: (defonce app-state (reagent/atom {:items [] :mapper pulagjite-mapper})) (defn get-trainers [] (GET "/trainers" {:handler #(swap! app-state assoc :items (:body %))})) (defn trainers-click-handler [e] (println "trainers clicked") (swap! app-state assoc :mapper trainers-mapper) (get-trainers))

p-himik 2023-04-06T14:55:03.537679Z

That looks alright.

bibiki 2023-04-06T14:56:06.645029Z

right, but the view is not alright. for some reasinf, my trainers-click-handler is not resulting in a clear view with trainer items only

bibiki 2023-04-06T14:56:48.775849Z

rather, it shows items of other types also. now, i do not retrieve items of other types from the db. I have them hardcoded in my other-type-click-handler

bibiki 2023-04-06T14:57:09.686199Z

and so other type click handlers work fine, but not trainers-click-handler

p-himik 2023-04-06T14:57:57.346309Z

I cannot provide any useful answer when there's no code.

bibiki 2023-04-06T14:58:01.613329Z

do you have any code that retrieves data from db and puts it in a reagent atom for it to be shown in the UI

bibiki 2023-04-06T14:58:55.453119Z

if you have that kind of code that works fine, then please show me how you retrieve the data and how you put it in the state atom

p-himik 2023-04-06T14:58:58.793249Z

The Reagent repo has plenty of examples. Your code above is nothing special, it looks just fine. Most likely, the error is either in the view or in the version of the code that the app is using (maybe a hot-reload or a cache issue).

bibiki 2023-04-06T14:59:55.459749Z

I ooked at reagent repo but I did not find any example that uses ajax.core to get data and show it in UI

p-himik 2023-04-06T15:01:06.570569Z

ajax.core is a very specific thing. You just need an example that changes a ratom asynchronously, that's it. Doesn't matter whether it's because of a response from ajax.core, or response from js/fetch, or file reading, or WebSocket, or whatever else one might come up with.

bibiki 2023-04-06T18:20:35.619649Z

it was my failure to set keys properly for some elements that was giving the problem. thanks for trying to help though.

p-himik 2023-04-06T18:28:58.089259Z

And that's why we usually ask for the full code. :)

👍 1
2023-04-06T20:14:21.531299Z

Hey there. What development workflows do you guys use for Reagent apps? I use the ClojureScript REPL that one gets with cider-jack-in-cljs, I use it for inspecting values, but I'm always in the context of the main app. I've seen some people mounting components manually and working on them individually (devcards style), I haven't done this yet, although it seems like an interesting approach. For more complex data structures I like to use (tap> value). and check them out in ShadowCLJS inspector, the one that starts on port 9630. (It shows up in the Inspect Latest tab.) I got some Rich comments with some manual setup, random stuff that helps me with development (.setItem js/localStorage "keep-open" true) etc, all ad-hoc. My screen is just 13", so if I can avoid it I try not to use Chrome dev tools (and splitting screen either vertically or horizontally. I essentially use them only when I need to inspect element, styles etc; so I try to stay in Emacs as much as possible. I feel like ClojureScript apps will inherently miss on many of the coolest parts of RDD, like I can't start a new REPL in a catch block with local context; because the runtime that blows up is the browser JS engine. (So I just went with nREPL, otherwise I'd prefer socket REPL which can be nested like this.) Do people use devcards? I've seen it, but haven't got to trying it out yet. Look like it might be useful, but I'm not 100% convinced yet. Anything else, tools & techniques that are worth of exploring?

p-himik 2023-04-06T20:18:16.048949Z

FWIW, DevTools can be popped out into their own window. I don't use REPL with UI development at all, only the console for all my logging needs and the fast feedback loop of hot code reloading with state preservation. Regarding starting a new REPL in a catch block - not the same of course, but I substitute it with "Break on all exceptions" when I debug something.

2023-04-06T20:19:46.219759Z

They do. I don't do that, because I have my apps in full-screen, so opening dev tools in a different space is useless then when I do want to inspect elements. Would I have two screens, I'd be using that, but I don't.

2023-04-06T20:20:32.907709Z

@p-himik "Break on all exception" is an attribute of what? Dev tools?

p-himik 2023-04-06T20:21:13.997889Z

DevTools, yeah.

2023-04-06T20:33:41.283209Z

@p-himik I wasn't aware. I'll try.

p-himik 2023-04-06T20:34:45.462769Z

Oh, just in case - there's also cljs-devtools.

2023-04-06T20:38:11.552719Z

Oh yeah. We used to go with darwin (author of cljs-devtools) to the first Ruby meetups in Prague back in 2007 I think it was. Good old times! Will check them out.

hifumi123 2023-04-06T20:59:14.680919Z

I personally use devcards and cljs-devtools. The nice part about devcards for me is the fact that I have a dedicated, isolated page to break and prototype whatever I want. I've been taking a look at portfolio as an alternative, though, since it is able to test responsiveness without compromises or extra setup cljs-devtools is mostly useful for being able to pretty print cljs data structures to a REPL

hifumi123 2023-04-06T20:59:51.804049Z

for data visualization i recall seeing some relatively new thing that'd let you tap> data and see a pretty picture of it in a browser tab; I forgot its name though

p-himik 2023-04-06T21:00:32.466699Z

Maybe Portal or Reveal?

hifumi123 2023-04-06T21:01:15.700919Z

portal looks close to what i saw several months ago; that may be it

2023-04-06T23:26:14.376639Z

@hifumi123 @p-himik Isn't it the Shadow CLJS inspector? It uses tap> and exactly shows it pretty printed in a tab of their inspector UI (which starts on a different port).

2023-04-06T23:27:20.580179Z

It's pretty cool, very readable. I like cljs-devtools a lot, but I wouldn't want to inspect something too large in it; this on the other hand has more space to it and is navigable, you can dive into the values.

p-himik 2023-04-07T05:43:08.038219Z

Yes shadow-cljs also has it. There are more than one tool. :) JS console is indispensable when working with JS values a lot.