Fork me on GitHub
#re-frame
<
2020-01-06
>
p-himik12:01:40

Sometimes I see people using subscribe inside :on-click or other JS event handlers, and I sometimes catch myself doing the same. But, if I'm understanding the code correctly, it creates a memory leak and gradual performance degradation in the same way using subscribe in a re-frame event handler does. I couldn't find any mentions of it in the docs. Am I blind, or should it be added there?

mikethompson12:01:14

Yeah, i agree. It should be documented

shaun-mahood22:01:12

I opened an issue so this doesn't get lost, will write up a new FAQ entry in the docs for it if nobody else wants to.

Ramon Rios16:01:06

Have you guys tried to loop a event? I need to loop a rest call for each element on a map.

p-himik16:01:23

You mean something like

(doseq [[k v] m]
  (dispatch [:ev k v]))
?

p-himik16:01:01

Or multiple :http-xhrio inside a single event handler?

Ramon Rios16:01:13

The first option

p-himik16:01:55

It will work, but it's not a great solution if you run the code in response to some user action. You should put the loop inside the event handler.

Ramon Rios16:01:03

I was thinking in loop in the :http-xhrio

Ramon Rios16:01:15

Is it possible?

p-himik16:01:13

:http-xhrio accepts either a map for a single request or a sequence of maps for multiple requests.

Ramon Rios16:01:04

thanks, i'll check that

Ramon Rios16:01:31

Let me see if i understood: With :http-xhrio i can send multiple request just passing a map with the requests?

Ramon Rios16:01:02

I have my map with multiple customers, and with this guy the http-xhrio will send one request for each one?

p-himik16:01:28

By just passing a vector of maps.

Ramon Rios16:01:06

[:main {:street "name" :customer ..uuid..} :facturation {:street "name" :customer ..uuid..}]
For example

p-himik16:01:05

What is this?

Ramon Rios16:01:23

This is a vector with maps inside

p-himik16:01:25

Have you used :http-xhrio before?

Ramon Rios16:01:34

Just for one simple request

Ramon Rios16:01:29

(rf/reg-event-fx
 :customer/save-addresses
 (fn [{:keys [db]} [_ cust-id]]
   (let [addresses (setting-address-id (d/addresses db) cust-id)]
     {:http-xhrio {:uri "api/addresses"
                   :method :post
                   :format ajax/json-request
                   :response-format ajax/json-response
                   :params addresses
                   :on-success [:customer/addresses-success]
                   :on-failure [:customer/addresses-failure]}})))

Ramon Rios16:01:37

This is the one that i'm doing now

p-himik16:01:45

That's not a vector of maps. That's a heterogenious vector. You pass to :http-xhrio a single map that describes how to make this XHR, right? Now create a map for each request. Put all these maps in a vector. And pass that vector into :http-xhrio.

Ramon Rios16:01:07

In :params right?

p-himik16:01:27

In :http-xhrio.

p-himik16:01:13

Every map passed to :http-xhrio is a single request. Everything within that map is relevant only for that request.

p-himik16:01:07

You should end up with something like

{:http-xhrio
 [{:uri "..."
   ...}
  {:uri "..."
   ...}
  ...]}

Ramon Rios16:01:19

{:http-xhrio [ {...}
               {...}]}

Ramon Rios16:01:37

I got now, many thanks

👍 4
Ramon Rios17:01:00

I created a def with the request and i'm going to see a reduce or something like that for create new lists with the multiple params that will be sent on these requests.

Eric Ihli20:01:45

Anyone know of any good resources on a good way to do navigation in re-frame? How to integrate the event/effect stuff with something like React Native Navigation? I'm currently doing this.

(this-as this <...> {:on-press (fn [] (. (.. this -props -navigation) navigate "edit"))} <...>)
But I could see it being "better" to do something like this.
{:on-press (fn [] (rf/dispatch [:navigate "edit"]))}
It looks like the Status project is doing the latter https://github.com/status-im/status-react/blob/develop/src/status_im/events.cljs#L489.
(handlers/register-handler-fx
 :chat.ui/navigate-to-chat
 (fn [cofx [_ chat-id opts]]
   (chat/navigate-to-chat cofx chat-id opts)))

p-himik20:01:35

https://github.com/ingesolvoll/kee-frame deals with navigation. But I have no idea whether it's compatible with React Native.

Eric Ihli22:01:44

Interesting. That lead me to https://github.com/vikeri/re-navigate which looks like a useful reference.

AJ Jaro13:01:19

I’m using Day8 Async Flow to dispatch events when navigation changes occur. I store the route handler information and then check for matching routes to dispatch a set of events for. https://github.com/day8/re-frame-async-flow-fx