This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-01-31
Channels
- # announcements (22)
- # asami (19)
- # aws-lambda (4)
- # babashka (42)
- # beginners (43)
- # calva (28)
- # cider (1)
- # clerk (79)
- # clj-kondo (12)
- # clojure (47)
- # clojure-berlin (1)
- # clojure-brasil (1)
- # clojure-dev (12)
- # clojure-europe (40)
- # clojure-nl (2)
- # clojure-norway (5)
- # clojure-uk (3)
- # clojurescript (56)
- # clr (12)
- # conjure (8)
- # cursive (4)
- # datomic (78)
- # dev-tooling (6)
- # exercism (1)
- # fulcro (9)
- # hoplon (3)
- # jobs (3)
- # jobs-discuss (4)
- # lambdaisland (3)
- # leiningen (1)
- # london-clojurians (1)
- # lsp (125)
- # malli (32)
- # matcher-combinators (3)
- # nrepl (1)
- # off-topic (6)
- # pathom (39)
- # re-frame (13)
- # releases (2)
- # remote-jobs (3)
- # sci (7)
- # shadow-cljs (117)
- # sql (6)
- # squint (7)
- # tools-build (15)
- # tools-deps (12)
Hi all, what is the best way to debug, inspect and access to the state of a component? (something like react developer tools) thx
You haven't mentioned the UI library that you're using. But I'm pretty sure that none of the existing libraries offer anything like that. You can use React DevTools with React wrappers just fine. And closest we have to CLJS state observation is re-frame-10x and re-frisk if you use re-frame.
At the moment I'm just trying https://github.com/pitch-io/uix
this is using state with the hooks, but i saw other libraries (as the old version of Uix) are using atom to manage state. but how you can see the state of the atom of the single component?
Since it's not even a wrapper, React DevTools should definitely be sufficient. No clue about the older version of Uix, but IIRC Reagent stores its atoms within the state of an instances, so those are also observable by React DevTools. I might be wrong though, it's been some time.
with new Uix version I can see the state but it's quite confusing (but better than nothing )
old Uix works like reagent if i remember well (it's been a while for me with reagent), and i cannot access to the component state (that basically are atom)
Yeah, nah - React DevTools isn't really useful when trying to read Reagent state. It's either not that (in the case of r/with-let
) or attempts to study it, assuming I found the correct thing, lead to time outs of React DevTools.
What "the best" means depends on you though, so no idea whether it's even suitable for you.
Back in the day when I was choosing what to use, I spent pretty much a week reading about all available options and trying the most promising things out to see how they felt.
How do I update a text field with value in a form only on button press without having to store two values separately? Is it possible? My code:
(defn update-text-on-press []
(let [form-value (r/atom "")
text (r/atom "")]
(fn []
[:div
[:input {:type "text"
:value @form-value
:on-change #(reset! form-value (-> % .-target .-value))}]
[:button {:on-click #(reset! text @form-value)} "Update"]
[:p @text]])))
You can make :input
an incontrolled component and access its internal value when clicking the button.
And you can access that rendered :input
from within the :on-click
handler by using React refs.
Here's a mystery to me: I'm using the js/MediaRecorder
stuff to record video. It gives me back video data in chunks. Then I can construct a js/Blob
to concatenate the chunks to a Blob of data (js/Blob. chunks #js {:type <mime-type>})
That blob is my video. If I conj the chunks on a vector and then give that vector to the constructor the data gets corrupted. If I convert the vector of chunks (clj->js chunks)
and give that to the constructor, the data also gets corrupted. But if I use a JS array and mutate it with (.push js-chunks)
as the data comes in, then the resulting blob is just fine. Why doesn't it work with a cljs vector, not even if I cast/convert it to a JS array?
Weird. clj->js
does exactly that to vectors - it creates an array and then calls .push
on it for every element.
Maybe there's something terrible in your codebase that extends the IEncodeJS
protocol to blobs. Apart from that, can't imagine what's going wrong. Would be curious to poke around if you can create an MRE.
That was my assumption. I'll create a repro. Might find out what I'm doing wrong when I do that. That often happens to me.
I usually see clj->js
as an anti-pattern, as it's a bit magical and often not the most performant solution
Gotcha. You can also create an array conventiently via into-array
clj->js
"helpfully" does more, like recursively converting elements, which can be risky
You shouldn't. :) Most of the time, you can do just fine without.
E.g. creating an array in one go from an iterable (e.g. a vector): (js/Array.from v)
. You can even map a function over it while converting: (js/Array.from v inc)
.
For arrays this is the first time I have even tried clj->js, I think. It's usually maps I do it with.
I don't think there's anything that's that easy for objects, but it's like 3 lines of CLJS code - I'd put it into some util NS and use it everywhere.
I'd probably reach for something like
cljs.user=> (apply js-obj (mapcat identity {"a" "b", "c" "d"}))
#js {:a "b", :c "d"}
@U0ETXRFEW I'm now dubious it has worked with a manual .push
. :) Maybe a fluke?
If converting a map to a JS object is in any performance-sensitive area, I'd go with reduce-kv
and gobj/set
to avoid building an extra coll.
The difference is in ondataavailable
on the MediaRecorder
instance. If I conj
the data on a vector, I am screwed. If I .push
it on an array, things are dandy. (In case this wasn't clear.)
That really sounds like something else is going on
You can 100% store js objects in a vector
Is that vector in an atom at least? I hope you don't just (conj v blob)
and call it a day. :D
I was thinking the same ^^ 🙂
So in each data available event, you dispatch
a re-frame event that adds a blob to some vector in the app-db?
And when the recorder is done, presumably you dispatch another event that gets all those blobs and does something to them?
Or do you, instead of using blobs as the medium, actually use JS events?
If it's the latter, this might be of interest: https://github.com/day8/re-frame/blob/master/docs/FAQs/Null-Dispatched-Events.md
Although it shouldn't be the crux of the issue because MediaRecorder
has nothing to do with React. But I'm very wary of using raw JS events in general.
It wasn't such a mystery after all. When I was to show @U06F82LES how it didn't work with vectors, it worked. No idea why it didn't when I switched to an array... But anyway, now functional again. Phew!
Could be, I have two phone clients and three desktop browsers, sometimes I lose track of where my REPL runs.
It started with me using nil
for my chunks and assuming conj
ing on that would append to the end. Then, without realizing that this wasn't the case, I tried with a vector because I felt sure that would work. But at a point I had a two stashes I could switch between and that reproduced the mystery. When showing @U06F82LES the problem I decided to not use the stash, but instead change it to a vector while we were pairing. I think I avoided some funny edit that I had in my vector stash.