Fork me on GitHub
#re-frame
<
2017-08-08
>
mikethompson01:08:58

@kenny I'm not aware of a way to keep local state

kenny01:08:51

I imagine there is probably a way to get it to work. Often times I have cases that really seem like they are so isolated. This causes me to ask myself "why pollute the global state with this?" Perhaps this is flawed thinking. Though, I cannot see myself running into any issues down the road with this.

mikethompson01:08:19

The conclusion I've come to: the more state is in one place the better

mikethompson01:08:12

The moment we start to distribute state ... there are complications

mikethompson01:08:30

Now, sometimes distributed state is unavoidable. So we live with it.

mikethompson01:08:36

But if you can avoid it ... do

mikethompson01:08:04

That's where my thinking is at these days (I always reserve the right to completely change my mind :-))

mikethompson01:08:30

So I wouldn't be thinking that you were "polluting global state". I'd control where you put this state within app-db, sure. But it's okay for it to go centrally.

kenny01:08:27

Out of curiosity, what are the complications that you have ran into? It seems like local state in an entirely independent component would be ok. This would give the user of this component one less thing to manage.

akiroz04:08:47

^ for me, the complications comes when I need to so something with that local state in another component... you never know what pieces of state the next feature your client wants will need. I find it easier to have everything in one place too.

yedi16:08:13

if i wanted to redirect to another page after a :login event gets handled successfully, whats the standard way of doing it in re-frame?

yedi16:08:55

in vanilla react, I would check in componentWillRecieveProps to see if the new props show that the user is now logged and then dispatch the redirect

yedi16:08:47

I could do something similar in re-frame with it's form-3 components, but I'm wondering if im missing something simpler

rnagpal20:08:07

@yedi in plain react way, I will not have a check in componentWillRecieveProps. On click of Login --> Make Ajax request --> Check for success/failure --> if Failure, then update props (you can update db and then subscribe) --> if success, then route to another URL (do the same in re-frame)

yedi21:08:14

thanks @rnagpal im attempting something lik ethat

yedi21:08:46

so i'm returing this effect dict in my event handler

{:dispatch-n (list [:status :login :success]
                       [:set-user user]
                       (when redirect-to [:change-url redirect-to]))}

yedi21:08:44

which ends up looking like this: ([:status :login :success] [:set-user {...}] nil)

yedi21:08:23

but I get this error: re-frame: you called "dispatch" without an event vector.

yedi21:08:43

but I thought you were allowed to have nil values in a dispatch-n list

yedi21:08:47

i got it to work by changing the code to:

(rf/reg-event-fx
  :successful-login
  [re-frame.core/debug]
  (fn [{:keys [db]} [_ redirect-to user]]
    (let [dispatch-list (list [:status :login :success]
                              [:set-user user])]
      {:dispatch-n (if redirect-to (conj dispatch-list [:url-change redirect-to]) dispatch-list)})))

yedi21:08:15

but appears that docs were incorrect about dispatch-n ignoring nil values