This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-04-04
Channels
- # aws (1)
- # beginners (163)
- # boot (1)
- # bristol-clojurians (1)
- # cider (7)
- # clara (1)
- # cljs-dev (22)
- # cljsjs (1)
- # clojure (43)
- # clojure-denver (1)
- # clojure-finland (6)
- # clojure-italy (1)
- # clojure-nl (3)
- # clojure-russia (1)
- # clojure-spec (1)
- # clojure-uk (6)
- # clojurescript (107)
- # cursive (4)
- # data-science (2)
- # datascript (2)
- # datomic (19)
- # duct (31)
- # emacs (1)
- # fulcro (50)
- # graphql (15)
- # hoplon (3)
- # lein-figwheel (2)
- # luminus (21)
- # off-topic (74)
- # onyx (3)
- # parinfer (15)
- # portkey (2)
- # precept (9)
- # proton (1)
- # re-frame (130)
- # reagent (73)
- # reitit (7)
- # ring-swagger (5)
- # shadow-cljs (61)
- # spacemacs (18)
- # specter (12)
- # uncomplicate (1)
- # vim (88)
- # yada (2)
So I have some view code (A table) that creates a new row whenever the last row has a value in any of its cells. It will also delete any row where all cells are empty. I plan on reusing this table. I’m tossing up whether or not to build this row creation & removal logic into events & subs or functions in view code. If I do events & subs, should I dynamically register many with some sort of unique key on component creation? or register once for all components and pass in a unique key when subscribing or dispatching?
Also what are your thoughts on storing local state for a view component in the on-create lifecycle method (of a form 2 component). Why would you choose local state over app db and vice-versa?
Hi, in reg-event-fx there are :db, :http-xhrio, :dispatch
. If I want to do some side effects like prn
is there any to key to use or do I have to create one?
@tomaas idk if I would consider logging to be enough of a side effect to make a seperate fx handler for it. I would just log in the event handle. But yeah as far as I know there is no logging fx. You would need to create it.
so i was looking for some handler which in general does some side effect work silently that nobody cares of (does not need to dispatch anything once it completes)
Standby for a new release of re-frame-10x
. Final touches being added. Not long. Maybe one more sleep.
I'm stealing Daniel's thunder but ... a teaser ...
Did a screenshot appear here ^^^ for everyone. I'm getting a message about not enough space.
I'm using re-frame
, but I'm having problems with subscriptions: in my app, it seems some of the subscription handlers are not registered when I'm using them... I don't understand why that would be the case.
@mikethompson yes, it's there 🙂
@kurt-o-sys https://github.com/Day8/re-frame/blob/master/examples/todomvc/src/todomvc/core.cljs#L8
You are probably missing the necessary requires to actually pull in the namespaces
in one my ns
s, I have somthing like this:
(def inject-now (partial generate-fn (re-frame/subscribe [:now])))
oh, ok... let me try 🙂
so, basically, it's not enough to add the re-frame.core
namespace and do re-frame/subscribe
to add the subs, right?
https://github.com/Day8/re-frame/blob/master/docs/Basic-App-Structure.md#theres-a-small-gotcha
ok, trying again (conceptually, this is what I have):
my.subs
:
(ns my.subs
(:require [re-frame.core :as re-frame]
[my.db :as db]
[my.model :as m]))
(re-frame/reg-sub
:now
(fn [db]
(get-in db [::m/view :now])))
my.db
:
(ns my.db
(:require [my.model :as m]
[cljs-time.core :as time]))
(def default-db
::m/view {:now (time/date-time 2018 1 3)})
my-ns
:
(ns my-ns
(:require [re-frame.core :as re-frame]
[my.subs]))
(defn inject-now [c]
(c {:now @(re-frame/subscribe [:now]) }))
accessing parameter :now
in component c
gives me nil
. I expect a date... What am I missing?Have you done an :initialize-db
kinda event?
To actually put a value into app-db
See the two examples in the repo
yeah... that's there.
https://github.com/Day8/re-frame/blob/master/examples/todomvc/src/todomvc/core.cljs#L24
(defn init! []
(re-frame/dispatch-sync [:initialize-db])
in core.cljs
the weird thing is, it did work before I added the function inject-now
(but I'm refactoring a bit, and I don't get why subscriptions wouldn't work at that point)
Seems wierd ....
(def default-db
::m/view {:now (time/date-time 2018 1 3)})
what's weird?
It isn't valid clojure
oh, forgot a {
:
I notice you are using ::m/view
in different namespaces
(def default-db
{
::m/view {now ....}})
They won't be the same thing
That's why you are getting nil
right... I know, but that part seems to work, sec... it's a bit weirder (imo)
Absolutely won;t work .... given what i can see
no, it does work with these namespaced keywords.
I did work...
it's that function that makes things not work 😛
Let's see this:
Indulge me.
Chnage from ::m/view
everywhere to :m/view
As presented, your code can't wark. i promise
please no, it does work with ::m/view
. That's not the problem. It's something else
Shrug
It did, please listen... these namespace keywords did work.
honestly 😛
When I add (println @(re-frame/subscribe [:now]))
to my namespace give a value when I start the app
but that value is suddenly removed when loading the page (after login of the app). Will dive deeper into it.
(so, to say, that subscribe initially works, but for some reason, the value is set to nil)
I can only work with the code you have given. And it absolutely 100% won;t work because ::m/view
will resolve to different keywords in different namespaces. And that perfectly explainsthe nil
you are seeing.
You asked for help. You provided code. That's the problem.
it can work, check it clearly:
I use that ::m/view
in my db
and in my subs, I load that same namespace
using (get-in db [::m/view ...])
Can I suggest you try using re-frisk or re-frame-10x to look inside of app-db
so it's pointing to the same ns 🙂
doesn't work, since I'm using npm (react) libs and I have to add them using http://blob.tomerweller.com/reagent-import-react-components-from-npm
which makes 10x
not usable, unfortunately.
(I tried adding npms another way, didn't work - that's the only thing that worked so far)
(re-frame/reg-sub
:now
(fn [db]
(println ::m/view) ;; <-----
(println db) ;; <----
(get-in db [::m/view :now])))
so, (println @(re-frame/subscribe [:now]))
this works in my ns itself (not in a function)
in the function, it gives nil
I'm assuming you are using clj-devtools
just using the chrome dev console.
@kurt-o-sys bridging the gap here, can you show the ns
form of one of the files that uses the ::m
keywords?
yes, see above, sec.
(ns my.db
(:require [cljs-time.core :as time]
[my.model :as m]))
that?
(println @(re-frame/subscribe [:now]))
(def inject-now (partial inject-fn @(re-frame/subscribe [:now])))
on the println, it gives a value, using the inject-now
, it gives nil
(I have a helper function to inject subscriptions in components - similar to reacts hoc.
(something must be go wrong there... oh, maybe it's about when things are evaluated and availability of subs)
partial
will lock in an initial value of the subscription?
oh...
Have you explained further up thread what you're actually trying to do here?
yeah, sorry, I didn't. What I want to do is make a kind of 'injector for re-frame subscriptions', the same way one uses hocs with react, so for example, with react components injecting queries for the database (apollo - doesn't really matter)
(-> my-component
reagent/reactify-component
((query/all-services-hoc))
...
So, I'd like to do something like this:
(-> my-component
((re-frame/inject-now-subscription))
...
that inject-fn
works fine for re-frame/dispatch
, having problems with re-frame/subscribe
. But I'm getting closer, with that 'lock-in of the initial value'.
Yeah, I don't think you want to be derefing it in the initial load
You need to inject the reaction and only deref it once you're inside the rendering of the component
Failing to do this will lead to all manner of strange bugs, like the ones you described 🙂
that may solve the problem, let me check....
yeah, thx a lot @mikethompson and @danielcompton
still need to solve the specs of the original component... all 'subscribe'-specs are Reaction
, not date
anymore...
Yeah that's a thorny one, I've dealt with the same thing with core.async and Manifold deferreds
Not sure if there are any great solutions at the moment
oh, I was about to ask 🙂
I guess you could do a spec that peeked at the Reaction's value, but that would be pretty dirty
yeah...
and might be buggy
another solution is to compose into another function/component with the deferred vals?
Reagent's change tracking relies on you deferring values in a render function, so it knows when to re-render
as long as your derefs are happening then, you should be ok
You could also try and spec the value as it is produced by the subscription
right. or just keep deferring inside the function/component itself. Specs will be less clear, but well...
@kurt-o-sys sorry I was wrong before and a little terse. I'm currently knee deep in powerpoint and not enjoying it. Grumpy.
np... thanks a lot for your help! just having to explain it to (grumpy) people may help a lot 🙂
quicky: is there a spec for a re-frame
Reaction.
I doubt it. Its a deftype
internal to reagent
Easier to spec the contents which will be a value
right...
going for inner components, and spec'ing these. makes more sense, and looks less hacky.
Hey guys!
Is the following legit in re-frame?
(ns app.effects.modal
(:require
[re-frame.core :as rf]))
(defn remove-back-scroll! []
(-> js/document .-body .-classList (.add "modal-background-no-scroll")))
(defn set-back-scroll! []
(-> js/document .-body .-classList (.remove "modal-background-no-scroll")))
(rf/reg-event-db
:modal/active
;; Takes a modal id to be opened
;; Can also take an optional path to get modal id
(fn [db [_ id & [path]]]
(if id (remove-back-scroll!) (set-back-scroll!))
(assoc-in db (or path [:modal/active]) id)))
I feel like the way I introduce the following side-effects isn’t idiomatic
(if id (remove-back-scroll!) (set-back-scroll!))
Thanks in advance!
@leontalbot for side-effects, you probably want to read a bit on the event handler vs effect handler distinction in the docs
(rf/reg-event-fx
:modal/handle-back-scroll
(fn [_ [_ id]]
(if id (remove-back-scroll!) (set-back-scroll!))))
(rf/reg-event-fx
:modal/active
(fn [fx [_ id & [path]]]
{:db (assoc-in (:db fx) (or path [:modal/active]) id)
:dispatch [:modal/handle-back-scroll id]}))
@mikerod Thanks. Is that better? ^
@mikerod more like {:db ... :back-scroll :remove}
or {:db ... :back-scroll :set}
Setting the back scroll is a side effect, so it should go into an effect handler
reg-fx
@danielcompton I believe you meant to tag @leontalbot in that one
@danielcompton Thanks a lot! Would you mind sharing a bit more so I understand how to integrate this?
Take a look at the docs on effects, that’s a good place to start