Fork me on GitHub
#re-frame
<
2021-02-06
>
Wilson Velez11:02:37

hi, I have two selectors, both separate components, the content of last of them depends on the first one. To get its values or options there is a subscription, so if the first one changed I always cleaned the selection of the second. Now the requirements have changed I need to preserve the option selected if it is also present in options related to the first selector, for that I need to use a subscription in an event handler, because it’s not a simple sub but a small logic, how to do that? I was thinking in 3 options 1. move the logic of the subscription to a function that I can call from the sub and from the event 2. inject the subscription as in https://github.com/day8/re-frame/blob/master/docs/FAQs/UseASubscriptionInAnEventHandler.md 3. or when loading the second selector detect that the option it should select is not present in the options and dispatch an event to clean it? Thanks!

p-himik13:02:44

I would start with the first option.

👆 3
Wilson Velez22:02:26

@U2FRKM4TW @UE35Y835W thanks! I thought maybe that wasn’t very idiomatic.

Lu22:02:17

@U9BUENJ1F I used approach one for a middle sized project some time ago and it does work well.. especially if your state ‘schema’ is subject to change, so you will only need to update one ‘getter’ and job done 🙂

Wilson Velez22:02:19

yes, I think the same way

Mikko Koski12:02:59

For quite some time I was doing option 1. However, I kept facing this issue again and again and this week I had enough :) It's kinda waste of time to first build your subscription chain and then at some point understand that you need this value in an event handler which means that you need to refactor all the subscriptions and extract the computation functions. And of course, things get more complicated with longer subscription chains. So I finally decided to install re-frame-utils and start using the inject-sub. I've been very happy with it. I'd maybe recommend to skip the option 1 and move straight to 2, you'll eventully want to do that anyway :)

Wilson Velez16:02:25

thanks @U0K7B1LM9, I’ve done the 1st one already, this is a small page/component so maybe this is not going top happen any time soon, but next time I know where to look